StackOverflowNewbie
StackOverflowNewbie

Reputation: 40653

jQuery: how to add an element after a text node?

Say I have the following markup:

<h1>Hello</h1>

and now, I want to add <span id="foo" class="bar">world</span> after Hello so that I have I have the resulting markup:

<h1>Hello<span id="foo" class="bar">world</span></h1>

How do I do this in jQuery?

Upvotes: 1

Views: 3689

Answers (3)

Tanveer
Tanveer

Reputation: 2107

Hi you can do it simply by using .append statement i.e.

 $('#heading').append($('<span />').text(' World')); 

as illustrated here http://jsfiddle.net/R5CpP/1/

Upvotes: 0

Tats_innit
Tats_innit

Reputation: 34117

demo http://jsfiddle.net/3zc3N/3/ few ways you can achieve this or http://jsfiddle.net/3zc3N/6/

code

$("h1").html($('h1').text() + ' <span id="foo" class="bar">world</span>');

or

the one above: http://jsfiddle.net/3zc3N/

$("h1").append(' <span id="foo" class="bar">world</span>');

Upvotes: 0

Esailija
Esailija

Reputation: 140234

$("h1").append( '<span id="foo" class="bar">world</span>' )

Upvotes: 7

Related Questions