Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the parent tr of a td using jQuery?

Tags:

jquery

I have the following:

    tdToPad = tds.filter('[id^="input_Title_"]')     pad = 60;     tdToPad.css('margin-left', pad); 

What I would like to do is to remove any class that starts with "X-" and give the row that is contained by "tdToPad" a class of "X-" + pad.

Something like:

<tr class='X-60'>    <td>    </td> </tr> 

Being that toToPad refers to the td element in a row. How can I give the parent tr the class "X-" + pad ? I think I need something like the following but if that's the correct way to do it then how can I remove elements already there with a class of "X-" somevalue and then give this element the correct class?

tdToPad.Parent('tr') 
like image 768
Alan2 Avatar asked Jul 21 '12 08:07

Alan2


People also ask

How to get parent tr id in jquery?

click(function() { console. log($(this). closest("tr"). attr("id")); });

How do I get parent TR?

Use closest() method to get the closest parent element matching the selector. closest() - Gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

How can get current TR data in jquery?

$(this). closest('tr'). children('td:eq(0)'). text();


2 Answers

You can use closest() method:

Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

tdToPad.closest('tr')        .addClass('X-' + pad) 

update:

tdToPad.closest('tr').get(0).className = tdToPad.closest('tr').get(0).className.replace(/\bX\-.*?\b/g, ''); tdToPad.closest('tr').addClass('X-' + pad) 
like image 119
undefined Avatar answered Sep 22 '22 11:09

undefined


You're almost right. Just use the correct spelling for parent() (docu) and add a addClass() (docu) call to it.

tdToPad.parent('tr').addClass( 'X-' + pad ); 
like image 24
Sirko Avatar answered Sep 21 '22 11:09

Sirko