How to get the class of an input inside a jQuery each loop?

Posted by Paul Atkins on Stack Overflow See other posts from Stack Overflow or by Paul Atkins
Published on 2011-03-09T23:38:38Z Indexed on 2011/03/10 0:10 UTC
Read the original article Hit count: 131

Filed under:
|

Hi,

I have function which appends inputs inside a list item when a link is clicked. I then loop through these inputs using an each loop using the code below. It is working correctly as shown, however instead of using field.name I want to use the class of the input as the array key but when i try to do this the class is shown as undefined.

Here is the code I am currently using:

var values = {};
$.each($('li :input').serializeArray(), function(i, field) {
    values[field.name] = field.value;
});

Here is the code I have inside the list item once I have appended the hidden inputs using jQuery append:

<li><input type="hidden" name="group" class="group" value="2"/><input type="hidden" name="condition" class="condition" value="isany"/><input type="hidden" name="value" class="value" value="1,2"/></li>

I can get the name attribute fine but class is always undefined.

Could anybody help with this?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about loops