tags:

views:

33

answers:

2

I want to count how many divs with the class .tool that contain the following html by 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 to do that using jQuery? i only can count all .tool divs?

Thanks

+3  A: 

Use filter:

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

If you insist on matching the HTML:

var count = $(".tool").filter(function() {
    return $(this).html() == '<b>Photoshop</b>';
}).length;
karim79
`$.map` will return an object with the same length as the *all the matched elements*, I think you want to use [`$.filter`](http://api.jquery.com/filter/)
CMS
@CMS - you are right, I stand corrected. Thanks for the comment.
karim79
Thanks a bunch karim :)
David
A: 

or use contains

$("div.tool:contains('Photoshop')").length
Paul Groves
That would also match 'Photoshop Plugins', 'Photoshop Tutorials' etc. That said, it might do for the OP's problem.
karim79
True, and true :)
Paul Groves