I am iterating over input elements using .each() and want to get class of each input element.
$("input").each(function(index,element){
// Here I want to access class value of current input ie. element here (I guess)
});
For example
HTML
<input name="name1" class="class1" value="input1">
<input name="name2" class="class2" value="input2">
I tried following selectors in .each but getting return as undefined
1. alert($('this').attr['class']);
2. alert($(this).attr['class']);
3. alert(element.class); // I can access name using element.name
4. alert($(element).attr['class']);
Jsfiddle : http://jsfiddle.net/kGjr2/
How can I access respective input and its properties inside .each()?
Thanks!
Really amazed by the quantity and quality of answers and that to within a really short time. Just wondering which answer should I accept. Thanks you all.
Just use parenthesis instead of square brackets… you are calling functions:
You can also select the elements by class:
Here is an updated version of your JSFiddle: http://jsfiddle.net/kGjr2/1/
Update
The reason you are getting
undefinedalerted at you is because square brackets are for accessing a property of an object/array. But since you are using the square brackets on a function you aren’t going to find any property names that matchclass.