1

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?

3
  • I just drunkenly googled "jquery append html to node" Commented Jul 8, 2012 at 8:10
  • 1
    see the jsfidle example: jsfiddle.net/R5CpP Commented Jul 8, 2012 at 8:15
  • 2
    Any proficient programmer knows what they want to do. You want to add html to an existing html element. It't not about what you know, it's about what you know to search for. Commented Jul 8, 2012 at 8:16

3 Answers 3

7
$("h1").append( '<span id="foo" class="bar">world</span>' )
Sign up to request clarification or add additional context in comments.

Comments

0

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>');

2 Comments

Your first example is a low level example of what you're 2nd example does.
@Nick Yep I was thinking any business logic might need more then that < I reckon > - thats why :) just lil extra info innit!
0

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/

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.