Changing Elements/Attributes

You can replace or modify your HTML using .html(). However, the more likely scenarios is to add more information to the HTML, remove something, or append other elements. Let’s look at these examples next.

Append/Remove Items

jQuery supports building parts of your HTML using code. Once you have a selection, you can use the methods .remove, .replaceWith(), .before, .after, .prepend, and .append. This is true if your selection is an element node or the HTML content.

For example, the following code appends an additional <li> item to the navbar list from Listing 18.1.

$('#navbar').append('<li>Import ride date from race computer</li>');

As another example, suppose you want to work with each ...

Get Microsoft Visual Studio 2015 Unleashed, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.