Hello,
i have this piece of code that counts the number of words in a td based off the class of the td. It works find if all the words inside the td's are the same, but if i change one, it doesnt work.

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    var totaltr = $(".ms-vb2")
    if(totaltr.html()== "link"){
        var htmlBlock = "<strong>" + totaltr.length + " ITEMS</strong>";
        totalProductList.innerHTML += htmlBlock;
    }
});
</script>
</head>
<body>
<table id="catlist_ul">
<tr><td class="ms-vb2">link</td></tr>
<tr><td class="ms-vb2">link</td></tr>
<tr><td class="ms-vb2">link</td></tr>
<tr><td class="ms-vb2">link</td></tr>
<tr><td class="ms-vb2">link</td></tr>
<div id="totalProductList"></div>
</table>

</body>
</html>

If i change on of the link to links, it doesnt write it? what can be going wrong?

you are only looking if the td content is equal to "link",

if(totaltr.html()== "link"){

if you put "links" inside, it wont enter the if and thus it wont add the "htmlBlock" to the "totalProductList.innerHTML"

Yes i know that, i want to be able to still add if a td has a different word in it

perhaps getting rid of the if will do that? im not sure i fully understand how that code is suposed to do what you want to do, in fact im not sure i understand what you are trying to do ! mind explaining a bit more?

Ok here is a better clarification:
Say i have 3 td's with different words in them:

<table>
    <tr>
        <td class="ms-vb2">links</td>
        <td class="ms-vb2">link</td>
        <td class="ms-vb2">link</td>
    </tr>
</table>

I want jQuery to count the words that i specifiy. Like if i wanted to know how many times dog shows up. The output should be
Link: 2
Links: 1

What i was trying to do is

$(document).ready(function(){
    //Find all the td's that have the class ms-vb2
    var totaltr = $(".ms-vb2"); 
    // Now, if that class has the word "link" in the innerHTML, it count it.
    if(totaltr.html()== "link"){
    //Print it
        var htmlBlock = "<strong>" + totaltr.length + " ITEMS</strong>";
        totalProductList.innerHTML += htmlBlock;
    }
});

What the problem is, if all the td's have the same word in it (like link), it count 3, but if i change one like in the first reference, it disappears and it doesnt work. I thought using the if statement would only count what i was looking for, like in PHP or something. Is this better clarification?

Member Avatar for stbuchok

Let me see if I have this straight (let's forget about tables and everything for the moment). You want to be able to count how many times each word is repeated. For example:

Word
Word
Char
Text
Text
Word

So Word would have a count of 3, Text a count of 2 and Char a count of 1, is that what you are trying to do?

yeah

I figured it out. The if statement wasnt what i was supposed to be using. jQuery has its own if

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    var x = $("td").filter('[class="ms-vb2"]');
    alert($('td:contains("link")').length);
});
</script>
</head>
<body>
<table id="catlist_ul">
    <tr><td class="ms-vb2">lin</td></tr>
    <tr><td class="ms-vb2">link</td></tr>
    <tr><td class="ms-vb2">link</td></tr>
    <tr><td class="ms-vb2">link</td></tr>
    <tr><td class="ms-vb2">link</td></tr>
</table>

</body>
</html>

Thanks for the help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.