0

I Have the JQuery code below. Basically trying to find the empty string in a td tag and add css to the tr tag, but it adds css to ALL rows not the row that has the empty string. Thank you in advance.

$('#mytable > tbody  > tr').each(function(){  
    var $nthis = $(this);

    $('td:first-child').each(function() {
        var $firsttd = $(this);
        console.log($firsttd.text());
    });

//back in the <tr> level
if ($firsttd.text() == "")
    $nthis.css('border-top','solid 2px blue'); 

}); //end of main func
2
  • thank you for your responses, i should have been clearer. each td tag contains an input tag. so i don't think i can use :empty, also not strictly the first td i'm looking for, any td in the tr that has .text() == "" Commented Nov 7, 2014 at 14:11
  • sorry i meant im looking for the FIRST td in the tr that has .text() == "" but for all rows Commented Nov 7, 2014 at 14:17

4 Answers 4

2

Use :first-child CSS3 selector to get the first td, and then :empty CSS3 selector to only retrieve the empty tds.

$(function () {
    $('#mytable > tbody  > tr td:first-child:empty').each(function(){  
        $(this).parent().css('border-top','solid 2px blue');
    });
});

http://jsfiddle.net/7n20ast0/

You can make it even shorter by removing the each loop:

$('#mytable > tbody  > tr td:first-child:empty')
        .parent().css('border-top','solid 2px blue');

http://jsfiddle.net/7n20ast0/1/

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

Comments

2

Try this : iterate first td of each tr and check if text() is empty. If empty then goto tr using parent() and change css.

$('#mytable > tbody  > tr td:first-child').each(function(){  
//check if td having empty text
if ($(this).text().trim() == "")
   {
    //get parent of td and put css
    $(this).parent().css('border-top','solid 2px blue'); 
   }
});

DEMO

5 Comments

I think he wants :first-child, not :first :)
:first-child means first child of parent, see this and :first do the same
Well, I think he tried to add a border to each tr that has it's first td empty, not only to the first tr. His question is ambigious. Using :first adds only one border, using :first-child adds as many borders as there are empty first tds.
your right Cristy, i want to check the first td in the tr, but for all rows
@Cristy and Shyamo, yes you are rigth :first will pick only first td and not the first td of each tr. I have updated my answer, please check. Thanks :)
0

use

if ($firsttd.text() == "")
    $(this).parent().css('border-top','solid 2px blue'); 

Comments

0

there is a jquery pseudo selector for empty elements:

$('#mytable > tbody > tr').find('td:first-child:empty').each(function(){  
var $nthis = $(this).parent();

$nthis.css('border-top','solid 2px blue'); 

}); //end of main func

Comments

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.