O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appending Elements to Parent Content

var newP = $("<p></p>"); newP.html("This is the first paragraph."); $("div").prepend(newP); newP.html("This is the last paragraph."); $("div").append(newP);

You can also use the .prepend() and .append() methods in jQuery to prepend or append content to the element. Another major advantage of jQuery is that you can append HTML DOM elements created using the jQuery constructor jquery(htmlString) or $(htmlString) to add new HTML elements to the page. For example, the following code constructs a jQuery object with a <p> element, sets the content of the new paragraph element using .html(), and then appends the paragraph to all <div> elements.

var ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required