James Huckabone
James Huckabone

Reputation: 625

jQuery - How to select elements containing only certain other element

How do I select the nodes that have <a name="something"></a>, so that I can bold its contained text?

<p>
  <a name="test"></a>
  Test 1
</p>

<p>
  <a href="test2"></a>
  Test 2
</p>

<div>
  <a name="test3"></a>
  Test 3
</div>

Desired output:

Test 1

Test 2

Test 3

Upvotes: 2

Views: 371

Answers (2)

Atif
Atif

Reputation: 10880

$('a[name="something"]').css('fontWeight', 'bold');

Upvotes: 1

VisioN
VisioN

Reputation: 145378

You can use .has() method:

$("p,div").has("a[name]").css("font-weight", "bold");​​​​​​​​​​​​​​​

or :has() selector:

$("p:has(a[name]),div:has(a[name])").css("font-weight", "bold");​

DEMO: http://jsfiddle.net/bWvwa/

Upvotes: 6

Related Questions