With jQuery, it is easy to add new elements/content. Show
Add New HTML ContentWe will look at four jQuery methods that are used to add new content:
jQuery append() MethodThe jQuery jQuery prepend() MethodThe jQuery Add Several New Elements With append() and prepend()In both examples above, we have only inserted some text/HTML at the beginning/end of the selected HTML elements. However, both the In the following example, we create several new elements. The elements are created with text/HTML, jQuery, and JavaScript/DOM. Then we append the new elements to the text with the Example function appendText() { Try it Yourself » jQuery after() and before() MethodsThe jQuery The jQuery Example$("img").after("Some text after"); $("img").before("Some text before"); Try it Yourself » Add Several New Elements With after() and before()Also, both the In the following example, we create several new
elements. The elements are created with text/HTML, jQuery, and JavaScript/DOM. Then we insert the new elements to the text with the Example function afterText() { Try it Yourself » jQuery ExercisesTest Yourself With ExercisesExercise:Use a jQuery method to insert the text "YES!" at the end of a <p> element. Start the Exercise jQuery HTML ReferenceFor a complete overview of all jQuery HTML methods, please go to our jQuery HTML/CSS Reference. Today, we’re going to look at four different techniques you can use to get and set text and HTML in DOM elements. Let’s dig in! The Element.innerHTML propertyYou can use the
Here’s a demo of the The Element.outerHTML propertyYou can use the
Here’s a demo of the The Node.textContent propertyYou can use the In the example below, you may notice that the Any HTML elements included in a string when setting content with the
Here’s a demo of the The Element.innerText propertyThe Unlike the Like
Here’s a demo of the Which one should you use?Generally speaking, if you’re only modifying text, using the For modifying HTML, the |