Becky
Becky

Reputation: 5585

Update text inside a div

How do I update <h2> text inside a particular div?

<div id="outerwrapper">
  <div id="wrapper>
    <h2></h2>
  </div>
</div>

$('#wrapper').text("new text");

Upvotes: 1

Views: 94

Answers (4)

Md Mahfuzur Rahman
Md Mahfuzur Rahman

Reputation: 2359

Try this:

$('#wrapper h2').html("new text");

And you've missed a quotation here: <div id="wrapper>

Code should be like this:

<div id="outerwrapper">
    <div id="wrapper">
        <h2></h2>
     </div>
</div>

Upvotes: 0

joacomf
joacomf

Reputation: 72

Your javascript code must be $('#wrapper h2').text("new text"); you only have to add the required tag after the ID.

Instead of $('#wrapper').text("new text");

By the way that code line should be inside

<script></script>

And inside a function. E.g.

(function($){
     //your code here
 })

Or

$(document).ready(){
     //your code here
 })

Upvotes: -1

dotnetom
dotnetom

Reputation: 24901

You can use find to find child element of a wrapper:

$('#wrapper').find("h2").text("new text");

Or use descendant selector:

$('#wrapper h2').text("new text");

Upvotes: 2

guradio
guradio

Reputation: 15555

$('#wrapper').find('h2').text("new text");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="outerwrapper">
  <div id="wrapper">
    <h2></h2>
  </div>
</div>

$('#wrapper').find('h2').text("new text");

Use .find()

Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

Upvotes: 1

Related Questions