How do I select div that contains "my content"?
<table>
<tr>
<td class="ms-disc-bordered-noleft">
<div class="">
<div>some content</div>
<div>my content</div>
</div>
</td>
</tr>
</table>
what relation does that div has with td.ms-disc-bordered-noleft?
How do I select div that contains "my content"?
<table>
<tr>
<td class="ms-disc-bordered-noleft">
<div class="">
<div>some content</div>
<div>my content</div>
</div>
</td>
</tr>
</table>
what relation does that div has with td.ms-disc-bordered-noleft?
Share Improve this question edited Jan 10, 2013 at 21:56 Athapali asked Jan 10, 2013 at 21:54 AthapaliAthapali 1,0894 gold badges25 silver badges48 bronze badges 4- first div has empty class, two divs that contain the actual content have no divs-- these divs are dynamically created.. – Athapali Commented Jan 10, 2013 at 21:58
-
Is
<div class="">
important in your tricky question? – Roko C. Buljan Commented Jan 10, 2013 at 22:01 - I have no control over the markup or i would make it more elegant.. – Athapali Commented Jan 10, 2013 at 22:03
-
Sarika, you have to really explain, do you need the last element , or containing exactly the text
my content
. your question is so hard to decipher – Roko C. Buljan Commented Jan 10, 2013 at 22:08
6 Answers
Reset to default 5$('.ms-disc-bordered-noleft div:last')
$('.ms-disc-bordered-noleft div:eq(2)')
$('.ms-disc-bordered-noleft').find('div:eq(2)');
- or
$("div:contains('my content'):last");
return the droid...I mean div...you are looking for.
I have a feeling that $('.ms-disc-bordered-noleft div:last')
is your best option; my performance test shows that it is generally the fastest of the 4 proposals (FireFox prefers $('.ms-disc-bordered-noleft').find('div:eq(2)');
).
See http://jsfiddle/jhfrench/cfeZU for examples of the different selectors in use.
As for the second part of your question, that div is a 'descendant' of the td.ms-disc-bordered-noleft
element. More specifically, it is the child of the td's child.
One option as second child of inner <div>
:
var div = $(".ms-disc-bordered-noleft > div > div:eq(1)");
It's a parent node, you can traverse up an down using .parent()
and .children()
or .find()
How do I select div that contains "my content"?
$('.ms-disc-bordered-noleft').find('div').contains("my content");
IF I did not understand well your Q. ...you can use:
$('.ms-disc-bordered-noleft div').find('div:last');
^^-parent ^^-first children ^^-last div
Most direct route would be:
var div = $("div:contains('my content'):last");
As far as the relationship to the td goes, you could start with the above and work your way back.
var cell = div.closest('td');
These aren't necessarily the fastest options in terms of performance, but they are the shortest code. And I like short code.
.ms-disc-bordered-noleft div div:nth-child(2)
or
.ms-disc-bordered-noleft div div:last-child