Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 889
Snote Avatar asked Nov 29 '11 09:11

Snote


People also ask

Can you get multiple elements by ID?

The id must be unique. There can be only one element in the document with the given id . If there are multiple elements with the same id , then the behavior of methods that use it is unpredictable, e.g. document. getElementById may return any of such elements at random.

Can I select multiple ID in JQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

How do you select multiple elements in Javascript?

To select multiple elements, you can use the querySelectorAll() method. Just like the querySelector() method, you usually use it on the document object.

How do I get all elements ID?

Use the document. querySelectorAll() method to get all elements whose id starts with a specific string, e.g. document. querySelectorAll('[id^="box"]') . The method returns a NodeList containing all the elements that match the provided selector.


2 Answers

Just try to put all id's in selector separated by comma:

$('#1234, #2345, #3456')...

Code: http://jsfiddle.net/3df6W/

P.S. ID's shouldn't start with digits.

like image 58
Samich Avatar answered Sep 19 '22 12:09

Samich


Try this

$('#1234, #2345, #3456')
like image 20
M S Avatar answered Sep 22 '22 12:09

M S