Isis
Isis

Reputation: 4666

Compare HTML table cell contents in JavaScript

<table>
    <tr>
        <td class = "nowrap cr" id="cr1">123123</td>
        <td class = "nowrap ch" id="ch1">123123</td>
    </tr>
    <tr>
        <td class = "nowrap cr" id="cr2">123123</td>
        <td class = "nowrap ch" id="ch2">123123</td>
    </tr>
    <tr>
        <td class = "nowrap cr" id="cr3">467574</td>
        <td class = "nowrap ch" id="ch3">123123</td>
    </tr>
</table>

How do I set the font-weight: bold in tr where chID == crID

I have written JS code as below

$(function () {
    for (var i = 0; i < 20; i++)
    {
        if ($('#cr' + i).val() == $('#ch' + i).val())
        {
            $('#cr' + i).parent().css('font-weight', 'bold');
        }
    }
});

But it's not working.

Let me know how should I achieve desired output?

Upvotes: 1

Views: 535

Answers (1)

Pekka
Pekka

Reputation: 449475

val() is for input elements only. You need to use

if($('#cr'+i).html() == $('#ch'+i).html())

or, to compare the textual value only (which is probably what you want)

if($('#cr'+i).text() == $('#ch'+i).text())

Upvotes: 2

Related Questions