lowerkey
lowerkey

Reputation: 8345

How to use jquery to select all elements that have two specific attributes

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

Answers (4)

orlp
orlp

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

Stephen
Stephen

Reputation: 18984

Give them a common class:

HTML

<div id="first" class="all"></div>
<div id="second" class="all"></div>
<div id="third" class="all"></div>

jQuery

$('div.all').each(function(index){
    processid(this.id);
});

Upvotes: 1

Gabriele Petrioli
Gabriele Petrioli

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

wombleton
wombleton

Reputation: 8376

Something like this?

$('[id]:not(:empty)').each(function(i, el) {
  // do stuff
});

Upvotes: 2

Related Questions