how do I get the Id of elements having a same class?
i have a class .myclass
i want to get the id of all textbox [html] who have this class.
how i can do this.
开发者_如何学编程i need to do this in jquery
Another way:
var ids = $('.class').map(function() { return this.id; }).get();
http://jsfiddle.net/X3Nd7/
It works best if you are sure that all elements have an ID attribute. If not, the array will contain undefined
entries.
Reference: map()
, get()
$(function () {
var id = [];
$('.myclass').each(function () {
if (this.id) {
id.push(this.id);
}
});
});
$('.someClass').each(function(){ // <-- This is a cycle, where we go through all elements having class="someClass"
$(this).attr('id'); // <-- This contains id of a current element (in the cycle)
});
$(".myclass :text").each( function() { alert($(this).attr('id')); });
You may also want to consult the documentation at http://api.jquery.com/category/selectors/
精彩评论