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 and Prepending Text to a Basic HTML Element

$("#p1").prepend("*New* :"); $("#p1").append(". . .read");

Another common dynamic task is adding text to a basic HTML element such as a span or paragraph. jQuery makes this extremely simple using the .append() method. This method appends content to the end of the element’s current content. It doesn’t matter if the content is a string or other elements.

For example, the following code appends the text ". . .read" to a paragraph with id="p1":

$("#p1").append(". . .read");

You may also want to prepend text to the beginning of an element’s content. In jQuery, the .prepend() method prepends content to the beginning of the element’s ...

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