jQuery – Get All Divs Whose ID Starts With

On November 12, 2009, in Web Design, by shereen

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');
Tagged with:  

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

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

  2. shereen says:

    Brandon, agreed.

  3. mussarath says:

    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 says:

    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