Reputation: 8345
I have some markup where a lot of id's have an id attribute, as well as innerText. I want to select each of these elements, performing a function on the id.
How do I do that?
Upvotes: 0
Views: 169
Reputation: 117951
First select by a regular ID selector and then loop over that selection by filtering .text() non-empty.
$("[id]").each(function() {
if ($(this).text() != "") {
// do stuff
}
});
Upvotes: 0
Reputation: 18984
Give them a common class:
<div id="first" class="all"></div>
<div id="second" class="all"></div>
<div id="third" class="all"></div>
$('div.all').each(function(index){
processid(this.id);
});
Upvotes: 1
Reputation: 196296
If you are talking about selecting elements whose id (or some permutation of it) is included in its text then
$('[id]').filter(function(){
return $(this).text().indexOf( this.id ) >= 0; // the this.id should be altered to match the permutation you seek ..
}).css('color','red'); // turn those to red
After you comment to @lonesomeday (at the question comments) here is what to do ..
$('[id]').each(function(){
processid(this.id);
});
Upvotes: 0
Reputation: 8376
Something like this?
$('[id]:not(:empty)').each(function(i, el) {
// do stuff
});
Upvotes: 2