Novadar
Novadar

Reputation: 96

JQuery - Evaluate TR Class, Delete IMG from matching result

Been working on this for a while.

As part of a larger effort, I need to remove images which reside inside a particular TR class. I have a JSFiddle working (where I mashed up two different approaches I found on SO) but the problem is that the IF statement resolves true for TWO of the THREE TRs in the table but it deletes all of the images. Even the non-matching one.

I know this is probably very simple.

Two TR Classes:

<TR class=rowToClick>

and

<TR class=hiddenRow>

I need the image to be preserved for

<TR class=rowToClick>

I commented out the very simple JQuery block. Both produce the same results.

Thanks!

http://jsfiddle.net/DDjFz/148/

// if ($('TR').hasClass('hiddenRow')) {$('td > img').remove();}

$.each(jQuery("TR"),function(){
   if (jQuery(this).hasClass("hiddenRow")) {
       $('td > img').remove();
    }
});

Upvotes: 0

Views: 67

Answers (3)

Vishal
Vishal

Reputation: 2181

Try:

$.each(jQuery("TR"),function(){
   if (jQuery(this).hasClass("hiddenRow")) {
       $(this).find('td > img').remove();
    }
});

Working demo: http://jsfiddle.net/DDjFz/149/

Upvotes: 1

rajesh kakawat
rajesh kakawat

Reputation: 10896

try this one, working fiddle

 $.each(jQuery("TR"),function(){
   if (jQuery(this).hasClass("hiddenRow")) {
     $('td > img',this).remove();
   }
});

Upvotes: 0

techfoobar
techfoobar

Reputation: 66693

Your current selector 'td > img' will match all image elements which are direct descendant of all tds - instead of only those inside the matched tr.

Changing

$('td > img').remove();

to

$(this).find('td > img').remove();

should do the trick.

Upvotes: 0

Related Questions