2

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?

4
  • first div has empty class, two divs that contain the actual content have no divs-- these divs are dynamically created.. Commented Jan 10, 2013 at 21:58
  • Is <div class=""> important in your tricky question? Commented Jan 10, 2013 at 22:01
  • I have no control over the markup or i would make it more elegant.. 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 Commented Jan 10, 2013 at 22:08

6 Answers 6

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.net/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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the effort! Very illustrative!
3

One option as second child of inner <div>:

var div = $(".ms-disc-bordered-noleft > div > div:eq(1)");

1 Comment

@FelixKling Yes, that's always unpredictable :/
1

It's a parent node, you can traverse up an down using .parent() and .children() or .find()

Comments

1

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

1 Comment

$('.ms-disc-bordered-noleft').find('div').contains("my content"); would error; .contains() only works against a DOM element, not against a jQuery object (api.jquery.com/jQuery.contains).
1

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.

4 Comments

$("div:contains('my content')"); would also return that div's container.
$("div:contains('some content')");, for example, would return the last div and filter it for the 'some content' string...so $("div:last"); and $("div:last:contains('my content')"); return the same thing in this case...but $("div:contains('my content'):last"); works to return just the desired div
Never mind. I see what you're saying. Editing now. Again. LOL
I don't know why I'm giving you such a hard time! There are 10,000 ways to do this with jQuery. I'm just enjoying testing out the different ways I don't usually try.
0
.ms-disc-bordered-noleft div div:nth-child(2)

or

.ms-disc-bordered-noleft div div:last-child

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.