7

I'm using jQuery, I have a div with H2 tags

<div id="preload"><h2></h2></div>

I would like to know how to use jQuery to add a text within the <h2> tags

Final result should be:

<div id="preload"><h2>Some text here</h2></div>

I need to add the text without replacing the tags <h2></h2> in the jQuery script.

Any ideas? Many thanks!

2
  • What's the larger picture? There may be an easier way to do that Commented Aug 1, 2012 at 19:45
  • SomeKittens thanks for your comment...The larger picture was: I bought to had read in some place that it was possible to add text in an element without replacing the html tag already present ....so I wanted to have a confirmation if it really exist in this function. Commented Aug 1, 2012 at 20:09

4 Answers 4

14

Try something like this :

$("div#preload h2").html("Some Text Here")

See the above code in action for your sample HTML here

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

Comments

9
$("#preload h2").text('WOOt Woot');

Comments

6

Use .text

$("#preload h2").text("Some text here");

Also not that this must be within either $(document).ready(function () { ... }) or $(function () { ... }). I would use the latter as it saves typing.

$(function () {
    $("#preload h2").text("Some text here");
});

Comments

2
$('#preload h2').html('foo');

OR

$('#preload h2').text('foo');

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.