G. Demecki
G. Demecki

Reputation: 10586

In jQuery, how to find all elements matching given strings?

I'm trying to find out how to select all <span class="title"> elements on the page:

<div class="my-page">
    <span class="title">first</span>
    <span class="title">second</span>
    <span class="title">third</span>
    <span class="title">fourth</span>
    <span class="title">fifth</span>
</div>

that are present on the list (fetched from servlet in JSON format) and stored inside some variable?

I've tried something like:

success : function(data, statusText, jqxhr) {
  var arr = data.selectedTitles;
    jQuery.each(data.favTools, function(i, v) {
      var title = v;
      jQuery('span.title:contains(title)').css("text-decoration", "underline");
    });
}

Upvotes: 0

Views: 628

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318518

$('span.title').filter(function() {
    return $(this).text().indexOf(title) != -1;
});

Upvotes: 10

Related Questions