I need to get all the ids for buttons. How to do that using jquery?
for examp开发者_如何学Pythonle,
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.
- Neelagandan
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()
according to jAndy answer:
var ids = $('form input:button[id]').map(function(){
return this.id;
}).get();
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.
精彩评论