Count how many divs have specific content?

I want to count how many divs with a class .tool

that contain the following html, for example:<b>Photoshop</b>

<div class="tool"><b>After Effects</b></div>
<div class="tool"><b>Photoshop</b></div>
<div class="tool"><b>Illustrator</b></div>
<div class="tool"><b>Photoshop</b></div>
<div class="tool"><b>Photoshop</b></div>
// This would return 3

      

How can I do this with jQuery? Can I count only .tool

divs?

thanks

+2


a source to share


2 answers


Use filter

:

var count = $(".tool").filter(function() {
    return $(this).text() == 'Photoshop';
}).length;

      



If you insist on HTML compliance:

var count = $(".tool").filter(function() {
    return $(this).html() == '<b>Photoshop</b>';
}).length;

      

+7


a source


or use contains



$("div.tool:contains('Photoshop')").length

      

+2


a source







All Articles