user927797
user927797

Reputation:

How do you select an element in a variable in jquery

Assume the follow code

<div class="hello"> 
  <h2>I'm a header</h2>
</div>

<script>
foo = $('.hello');
</script>

How do I select the h2 tag from foo and replace it's contents?

I'm basically looking for the equivalent of $('.hello > h2).html('New header') but on the variable.

Upvotes: 0

Views: 54

Answers (4)

kroehre
kroehre

Reputation: 1104

To get a child of a jQuery object you can use children():

var foo = $('.hello');
foo.children('h2').html('New header');

Upvotes: 0

Praveen
Praveen

Reputation: 1449

I guess you could do something like below

foo = $(".hello h2");

Upvotes: 0

Clive
Clive

Reputation: 36957

Are you looking for:

foo = $('.hello');
foo.find('> h2').html('New Header');

Upvotes: 2

NimChimpsky
NimChimpsky

Reputation: 47280

var foo = $(".hello > h2")

jquery api

Upvotes: 0

Related Questions