Alex Rivera | Logout

Get multiple elements by a list of id's with jQuery

Asked 2011-11-29T09:12:28.230
10

I have that in my html

<input type="checkbox" id="1234">
<input type="checkbox" id="2345">
<input type="checkbox" id="3456">
<input type="checkbox" id="4567">
<input type="checkbox" id="5678">

And an list of id 1234 2345 3456 or #1234 #2345 #3456

I want to get all the element of the list whose id is in the list of id

I try $(":checkbox").attr('id', items_id); and var items_cb = $(":checkbox[id='items_id']"); where items_id is the list of item, but it doesn't work.

Edit
Report

1 Answer

1
var arr = ['1234', '2345', '3456'];
var elem = [];
$('body > input').filter(function() {
    if ($.inArray(this.id, arr) != -1) {
        elem.push({
            id: this.id,
            type: $(this).prop('type'),
            name: this.nodeName
        });
    }
});
console.log(elem);
console.log(elem.length);
answered 2011-11-29T09:58:28.323

Your Answer