HTML and CSS Reference
In-Depth Information
In addition to adding new content to the DOM, some manipulation functions operate on
characteristics of individual elements to either return information about them, or to manip-
ulate this information. For instance, to find the value of the select element in the document
we can execute:
> $('select').val()
The same function can be passed a value if we want to change the current value. If you
execute the following the value of the select field will immediately change:
> $('select').val('Work')
An important note regarding this call is that if any event listeners are registered
to detect changes to this element, they will not be notified with this call.
In order to notify any listeners of a change to the element, the following call
could be used:
$('select').val('Work') .change()
Event listeners will be covered in detail later in this chapter.
Other similar functions are:
• html : this returns or manipulates the html of an element.
• text : this returns the text (or content) of an element. The text is the content between the
elements tags.
• attr : this returns or manipulates the value of an attribute on an element.
These functions are also a little different from some of the others we have examined in that
they only operate on a single element. For instance, if you execute the following:
> $('div').html()
Only the HTML for the first element in the set of matched elements will be returned, rather
than the HTML of all elements. Some jQuery functions are inherently limited in this way.
In order to execute these functions on multiple elements, it is necessary to loop through
those elements. This will be shown later in this chapter.
Search WWH ::

Custom Search