Get the class and name of an Input

1

How do I get the class ( Class ) of the following code, making a condition with the values returned by the name attribute. That is, I want if the input has a given name in its name attribute it returns me in addition to the value of the attribute name its class.

I'm only able to return name of input but I want to get your class also based on the returned name. The condition would be: If input has name thiago / p>

<div><input type="radio" class="classe1" name="thiago" value="num1">numero1</div>
<div><input type="radio" class="classe1" name="thiago" value="num2">numero 2</div>
<div><input type="radio" class="classe1" name="thiago"value="num3">numero 3</div>
    
asked by anonymous 01.02.2018 / 15:02

2 answers

1

getElementsByName can be used to get elements with name="thiago" and then with the list of values get the value of the .class attribute with getAttribute ('class') , example :

Pure Purification

var els = document.getElementsByName('thiago');

for(i = 0; i < els.length; i++)
{
    console.log(els[i].getAttribute("class"));
}
<div>
  <input type="radio" class="classe1" name="thiago" value="num1">numero1</div>
<div><input type="radio" class="classe1" name="thiago" value="num2">numero 2</div>
<div><input type="radio" class="classe1" name="thiago" value="num3">numero 3</div>

JQuery

var els = $("[name=thiago]");
$.each(els, function(a,b){
    console.log($(b).attr('class'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div><inputtype="radio" class="classe1" name="thiago" value="num1">numero1</div>
<div><input type="radio" class="classe1" name="thiago" value="num2">numero 2</div>
<div><input type="radio" class="classe1" name="thiago" value="num3">numero 3</div>
    
01.02.2018 / 15:09
1

alert($("[name=ab]").attr("class"))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script><inputname="ab" type="text" class="all" value="1" />
    
01.02.2018 / 15:11