I
I
ionicman2011-10-20 15:15:11
JavaScript
ionicman, 2011-10-20 15:15:11

A question for the JQuery guru - why doesn't such a tricky selector work?

There is this html:

&lt;tr&gt;<br/>
 &lt;td onclick=&quot;$(':parent~tr.dsc:first',this).toggle()&quot;&gt;...&lt;/td&gt;<br/>
 &lt;td&gt;...&lt;/td&gt;<br/>
&lt;/tr&gt;<br/>
&lt;tr class=&quot;dsc&quot;&gt;<br/>
 ...<br/>
&lt;/tr&gt;<br/>


Meaning: on click on TD, hide/show tr with dsc class.
If you do the same with parent() or next() then everything works.

From the point of view of semantics, we got the parent (TR), from it we went to look for the next sibling (TR.dsc) and took the first element from the collection - everything seems to be correct.

Climbing to look at JQ is not much of a hunt, can anyone come across?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2011-10-20
@taliban

$(':parent', elem)
looks for _inside_ the element of its parent, this is not correct. Make it easier:
$(this).parent().next().toggle();
It will be faster (no parsing of selectors)
It will be easier (when changing, you don’t have to rack your brains understanding selectors)
It will work =)
+ in each of the methods you can insert a selector for filtering, if necessary

M
mark_ablov, 2011-10-20
@mark_ablov

this is not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question