3

I have the following structure of row in my table:

<tr class="mattersRow">
    <td></td>
    <td colspan="16">
        <div class="dropdown">
            <a href="#" data-toggle="dropdown">Choose QB</a>
            <ul class="dropdown-menu" aria-labelledby="dLabel" role="menu"> … </ul>
        </div>
    </td>
</tr>

Note: I use bootstrap library

I need to set to every <td> that has text inside "" (empty), or "Choose QB" class='danger'.

This is my jQuery code:

$("#tblMatters .mattersRow td").each(function () {
      if ($(this).html() == "" || $(this).innerHTML == "Choose QB")
      {
           $(this).addClass("danger");
           flag = true;
      }
});

Of course, $(this).html() == "" works great, but how to set "danger" class to the <td> with Choose QB

0

4 Answers 4

3

You may use :contains()doc and :emptydoc selectors :

$('#tblMatters .mattersRow')
    .find('td:contains(\'Choose QB\'),td:empty')
    .addClass('danger');

Here is a demo : http://jsfiddle.net/wared/a84CW/.

Sign up to request clarification or add additional context in comments.

Comments

3

addClass accepts a function as an argument, and within that function you have access to the currently iterated td element, and can check what it contains :

$("#tblMatters .mattersRow td").addClass(function() {
    var txt = $.trim( $('a', this).text() );

    return txt.length === 0 || txt == 'Choose QB' ? 'danger' : '';
});

If you're only trying to check the text inside the anchor, you should specify that in the question ?

2 Comments

Do you have other elements inside that TD with content, or in other words, shouldn't you probably just target the anchor only ?
@Bryuk - I made an edit to the answer, that should solve any issues.
1

Use $(this).text().trim() === 'Choose QB'

Comments

1

Try .closest()

$(this).closest('td').addClass("danger");

Use .filter()

$("#tblMatters .mattersRow td").filter(function () {
    var txt = $.trim($(this).text());
    return txt.length === 0 || txt === 'Choose QB';
}).addClass("danger");

or check text of anchor tag

$("#tblMatters .mattersRow td a").filter(function () {
    var txt = $.trim($(this).text());
    return txt.length === 0 || txt === 'Choose QB';
}).closest('td').addClass("danger");


This should work for both

$("#tblMatters .mattersRow td a").filter(function () {
    var txt = $.trim($(this).text());
    return $(this).closest('td').text().length === 0 || txt === 'Choose QB';
}).closest('td').addClass("danger");

7 Comments

This is how to set class, Thanks, but how to find that (this) contains "Choose QB"
Haha. Now Choose QB works, but Empty td doesn't... Of course, I can combine your code and my, but will be great if it will works together...
OK. Summary for right now: 1st - works only for EMPTY 2nd - works only for Choose QB 3rd - doesn't work for anything
@Bryuk i missed a in 3rd one try one more time updated .Than i;m done.
Thanks, wared gave good and simple code=) But your help was good!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.