jQuery – Get All Divs Whose ID Starts With

Structure:

1
2
3
4
<div id="thisismydiv1"></div>
<div id="thisismydiv2"></div>
<div id="thisismydiv3"></div>
<div id="thisismydiv4"></div>

To return an array of all divs that start with ‘thisismydiv’, you would use the following:

1
var divList = $("div[id^='thisismydiv']");

The above translates to, find me all divs whose id starts with ‘thisismydiv’. You could now do something cool like add a class to all those divs:

1
$("div[id^='thisismydiv']").addClass('myClass');

4 Responses to “jQuery – Get All Divs Whose ID Starts With”

  1. Brandon Buttars March 29, 2011 at 8:22 pm #

    You can do so much more when you combine jQuery with CSS.

  2. shereen April 1, 2011 at 12:01 pm #

    Brandon, agreed.

  3. mussarath September 19, 2011 at 3:09 am #

    Can any one tell me how to change the div name of the perticular div and disselect the rest of the divs ..or how to get all the div id names of the div ?

  4. shereen September 19, 2011 at 8:06 am #

    Hi mussarath, not sure I understand your question completely. Are you using a Name attribute on your divs? Is that what you’re trying to change? Maybe post some sample html with what you’re trying to achieve, and I can help.

Leave a Reply