Get HTML element with ID starting with certain string in JQuery

How to get a list of elements whose ID starts with a certain string?

E.g. I have the following code
<div>
<div id="name-1"></div>
<div id="address-1"></div>
<div id="name-2"></div>
<div id="address-2"></div>
</div>

and I want to get the DIVs whose ID starts with "name-".
graham on 15-02-2013 11:32
Use the ^= selector in JQuery:
$("div[id^='name-']").each(function() {
alert($(this)..id);
});
graham on 15-02-2013 11:34

Post an answer

Do you have a solution to this problem, or want to comment on it? Other users will appreciate your help!
Your answer or comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems