jQuery Home

DOM

Get Content

text()

Returns the text content of selected elements.


html()

Returns the content of selected elements (including HTML markup).


val()

The following example demonstrates how to get the value of an input field with the jQuery val() method.


attr()

The following example demonstrates how to get the value of the href attribute in a link.


Set Content

text()

Sets the text content of selected elements.


html()

Sets the content of selected elements (including HTML markup).


val()

The following example demonstrates how to set the value of an input field with the jQuery val() method.


attr()

The following example demonstrates how to set the value of the href attribute in a link.


Callback Function

Return the string you wish to use as the new value from the function.

text()

html()

val()

attr()

Add Elements

The jQuery append() method inserts content AT THE END of the selected HTML elements.




The jQuery prepend() method inserts content AT THE BEGINNING of the selected HTML elements.




Add Several New Elements.




jQuery after() and before() Methods.


Remove Elements

The jQuery remove() method removes the selected element (and its child elements).




The jQuery empty() method removes the child elements of the selected element(s).




The jQuery remove() method also accepts one parameter, which allows you to filter the elements to be removed.




This example removes all <p> elements with class="test" and class="demo".


jQuery addClass()

The following example shows how to add class attributes to different elements.

The headings and paragraphs will be assigned blue text.

The div will be given larger text size.


jQuery removeClass()

The following example shows how to remove a specific class attribute from different elements.

The headings and paragraph with blue text will have the attribute removed.


jQuery toggleClass()

The following example will show how to use the jQuery toggleClass() method. This method toggles between adding/removing classes from the selected elements.

The headers and paragraphs will alternate between acquiring/losing the blue text attribute.


css() Method

Return A CSS Property

The following example will return the background-color value of the FIRST matched element.


Set A CSS Property

The following example will set the background-color, font size and text color values for ALL matched elements: paragraphs.