jQuery website jQuery documentation

DOM / Attributes

attr(String name) : Object

nameStringThe name of the property to access.

Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element.

Example

Returns the src attribute from the first image in the document.

Before
<img src="test.jpg"/>
Code
$("img").attr("src");
Result
test.jpg

attr(Map properties) : jQuery

propertiesMapKey/value pairs to set as object properties.

Set a key/value object as properties to all matched elements.

This serves as the best way to set a large number of properties on all matched elements.

Example

Sets src and alt attributes to all images.

Before
<img/>
Code
$("img").attr({ src: "test.jpg", alt: "Test Image" });
Result
<img src="test.jpg" alt="Test Image"/>

attr(String key, Object value) : jQuery

keyStringThe name of the property to set.
valueObjectThe value to set the property to.

Set a single property to a value, on all matched elements.

Can compute values provided as ${formula}, see second example.

Note that you can't set the name property of input elements in IE. Use $(html) or .append(html) or .html(html) to create elements on the fly including the name property.

Examples

Sets src attribute to all images.

Before
<img/>
Code
$("img").attr("src","test.jpg");
Result
<img src="test.jpg"/>

Sets title attribute from src attribute, a shortcut for attr(String,Function)

Before
<img src="test.jpg" />
Code
$("img").attr("title", "${this.src}");
Result
<img src="test.jpg" title="test.jpg" />

attr(String key, Function value) : jQuery

keyStringThe name of the property to set.
valueFunctionA function returning the value to set.

Set a single property to a computed value, on all matched elements.

Instead of a value, a function is provided, that computes the value.

Example

Sets title attribute from src attribute.

Before
<img src="test.jpg" />
Code
$("img").attr("title", function() { return this.src });
Result
<img src="test.jpg" title="test.jpg" />

text() : String

Get the text contents of all matched elements. The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents.

Example

Gets the concatenated text of all paragraphs

Before
<p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>
Code
$("p").text();
Result
Test Paragraph.Paraparagraph

text(String val) : String

valStringThe text value to set the contents of the element to.

Set the text contents of all matched elements.

Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities).

Examples

Sets the text of all paragraphs.

Before
<p>Test Paragraph.</p>
Code
$("p").text("<b>Some</b> new text.");
Result
<p>&lt;b&gt;Some&lt;/b&gt; new text.</p>

Sets the text of all paragraphs.

Before
<p>Test Paragraph.</p>
Code
$("p").text("<b>Some</b> new text.", true);
Result
<p>Some new text.</p>

val() : String

Get the current value of the first matched element.

Example

Before
<input type="text" value="some text"/>
Code
$("input").val();
Result
"some text"

val(String val) : jQuery

valStringSet the property to the specified value.

Set the value of every matched element.

Example

Before
<input type="text" value="some text"/>
Code
$("input").val("test");
Result
<input type="text" value="test"/>

html() : String

Get the html contents of the first matched element. This property is not available on XML documents.

Example

Before
<div><input/></div>
Code
$("div").html();
Result
<input/>

html(String val) : jQuery

valStringSet the html contents to the specified value.

Set the html contents of every matched element. This property is not available on XML documents.

Example

Before
<div><input/></div>
Code
$("div").html("<b>new stuff</b>");
Result
<div><b>new stuff</b></div>

removeAttr(String name) : jQuery

nameStringThe name of the attribute to remove.

Remove an attribute from each of the matched elements.

Example

Before
<input disabled="disabled"/>
Code
$("input").removeAttr("disabled")
Result
<input/>

addClass(String class) : jQuery

classStringOne or more CSS classes to add to the elements

Adds the specified class(es) to each of the set of matched elements.

Examples

Before
<p>Hello</p>
Code
$("p").addClass("selected")
Result
[ <p class="selected">Hello</p> ]
Before
<p>Hello</p>
Code
$("p").addClass("selected highlight")
Result
[ <p class="selected highlight">Hello</p> ]

removeClass(String class) : jQuery

classString(optional) One or more CSS classes to remove from the elements

Removes all or the specified class(es) from the set of matched elements.

Examples

Before
<p class="selected">Hello</p>
Code
$("p").removeClass()
Result
[ <p>Hello</p> ]
Before
<p class="selected first">Hello</p>
Code
$("p").removeClass("selected")
Result
[ <p class="first">Hello</p> ]
Before
<p class="highlight selected first">Hello</p>
Code
$("p").removeClass("selected highlight")
Result
[ <p class="first">Hello</p> ]

toggleClass(String class) : jQuery

classStringA CSS class with which to toggle the elements

Adds the specified class if it is not present, removes it if it is present.

Example

Before
<p>Hello</p><p class="selected">Hello Again</p>
Code
$("p").toggleClass("selected")
Result
[ <p class="selected">Hello</p>, <p>Hello Again</p> ]