Johan Dahl
Johan Dahl

Reputation: 1742

Put attributes of jquery selector in array

I want to save the ID of each input element of the form which form element I clicked into an array.

    var element = $(element);
    var $formID = element.closest('form').prop("id") || "";
    var formElementsIDs = []; // What to do?

There, I got the form ID. How do I get each form element from here?

Upvotes: 0

Views: 39

Answers (2)

Milind Anantwar
Milind Anantwar

Reputation: 82251

You can use .map() and .get() with appropriate selector to get all the ids in array :

var ids=$(element).closest('form').find('input').map(function() {
    return this.id; 
}).get();

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

Try to use .map() along with .get() to collect all the input element's id in an array,

 var formElementsIDs = element.closest('form').find('input').map(function(){
   return this.id || '';
 }).get();

Upvotes: 2

Related Questions