Neelu
Neelu

Reputation: 31

How to get all Ids from form?

I need to get all the ids for buttons. How to do that using jquery?

for example,

I have two buttons in my form :

My requirement is, i need to get these buttons "id" using javascript or jquery? because that id is dynamically changing whenever form loads.

Upvotes: 2

Views: 581

Answers (3)

jAndy
jAndy

Reputation: 236032

Suggestions, .map().

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

That piece will return all button id's into the array ids. To prevent the necessary .get() call in this example, you can use jQuery.map() as well:

var ids = $.map($('form').find('input:button'), function(elem, i){
    return elem.id;
});

It's probably a very good idea to cache the wrapped set before calling map on it.

Ref.: .map(), $.map()

Upvotes: 5

Deviprasad Das
Deviprasad Das

Reputation: 4363

I would like to modify it a little

var ids = $('form input:button[id], form input:submit[id]').map(function(){
    return this.id;
}).get();

This code will include both Submit button and simple button.

Upvotes: 0

zerkms
zerkms

Reputation: 254944

according to jAndy answer:

var ids = $('form input:button[id]').map(function(){
    return this.id;
}).get();

Upvotes: 0

Related Questions