Accessing Selection Content

Your selected objects in jQuery are returned as a jQuery object that maintains a reference to the DOM node. This is different from objects returned by the document selectors. Therefore, jQuery provides its own methods for working with your selected results. Chief among them are .html(), .text()., and .val().

The .html() method returns the HTML inside the selected element. If more than one element is in your results, only the HTML contained inside the first element is returned. As an example, the following selection returns all the <li> items inside the <ul> in Listing 18.1.

var sel = $('#navbar').children('li');var ht = sel.html();

However, the call to .html() in the preceding code returns ...

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.