Take the class and name of an Input

Asked

Viewed 3,625 times

1

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

I’m only managing to return the name of input but I want to get your class also based on the name returned. The condition would be: If the input has the name thiago then return me your 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>

2 answers

1


Can be used getElementsByName to pick up the elements with name="thiago" and then with the list of values take the value of the attribute .class with getattribute('class'), example:

Javascript Puro

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>
  <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>

1

alert($("[name=ab]").attr("class"))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>


<input name="ab" type="text" class="all" value="1" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.