Cristian Boariu
Cristian Boariu

Reputation: 9621

why this jQuery does not work?

I have this table with some styled tds (I put screenshot because the content of td is pretty big) enter image description here

Now, I want to add some css to this table so I try to select its td by one if its classes and apply the style like:

jQuery(".graTabbedComponentDefaultTabs").parent('table').css('position','relative');

But nothing happens....Do you see the problem?

Thanks in advance.

Upvotes: 0

Views: 91

Answers (5)

StuperUser
StuperUser

Reputation: 10850

Because the parent of elements with that class are <tr>s and not a <table>, your parent('table') call will return nothing.

Use Closest. NOT parents, as this may select a table that is the parent of that table.

$(".graTabbedComponentDefaultTabs")
    .closest('table')
    .css('position','relative');

Upvotes: 1

Peter Smeekens
Peter Smeekens

Reputation: 664

Use parents instead of parent:

jQuery(".graTabbedComponentDefaultTabs").parents('table').css('position','relative');

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227310

You want to use closest() instead of parent().

jQuery(".graTabbedComponentDefaultTabs").closest('table').css('position','relative');

parent() grabs the direct parent of an element (if it matches the selector). closest() traverses up the DOM until it finds the selector you want and then stops. parents() traverses up the DOM and grabs all elements that match the selector.

Upvotes: 5

mway
mway

Reputation: 4392

<table> is not the parent of your <td> - use .parents('table') instead to get the ancestor instead of the immediate parent.

Upvotes: 3

Rory McCrossan
Rory McCrossan

Reputation: 337713

You need to use parents(), not parent().

Parent() will look 1 level up in the DOM. Parents() will traverse up the DOM until it finds a matching element.

jQuery(".graTabbedComponentDefaultTabs").parents('table').css('position','relative');

Upvotes: 3

Related Questions