开发者

how do I get the Id of elements having a same class?

开发者 https://www.devze.com 2023-01-20 09:16 出处:网络
i have a class .myclass i want to get the id of all textbox [html] who have this class. howi can do this.开发者_如何学编程

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消