name | String | The 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.
Returns the src attribute from the first image in the document.
<img src="test.jpg"/>
$("img").attr("src");
test.jpg
properties | Map | Key/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.
Sets src and alt attributes to all images.
<img/>
$("img").attr({ src: "test.jpg", alt: "Test Image" });
<img src="test.jpg" alt="Test Image"/>
key | String | The name of the property to set. |
---|---|---|
value | Object | The 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.
Sets src attribute to all images.
<img/>
$("img").attr("src","test.jpg");
<img src="test.jpg"/>
Sets title attribute from src attribute, a shortcut for attr(String,Function)
<img src="test.jpg" />
$("img").attr("title", "${this.src}");
<img src="test.jpg" title="test.jpg" />
key | String | The name of the property to set. |
---|---|---|
value | Function | A 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.
Sets title attribute from src attribute.
<img src="test.jpg" />
$("img").attr("title", function() { return this.src });
<img src="test.jpg" title="test.jpg" />
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.
Gets the concatenated text of all paragraphs
<p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>
$("p").text();
Test Paragraph.Paraparagraph
val | String | The 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).
Sets the text of all paragraphs.
<p>Test Paragraph.</p>
$("p").text("<b>Some</b> new text.");
<p><b>Some</b> new text.</p>
Sets the text of all paragraphs.
<p>Test Paragraph.</p>
$("p").text("<b>Some</b> new text.", true);
<p>Some new text.</p>
Get the current value of the first matched element.
<input type="text" value="some text"/>
$("input").val();
"some text"
val | String | Set the property to the specified value. |
---|
Set the value of every matched element.
<input type="text" value="some text"/>
$("input").val("test");
<input type="text" value="test"/>
Get the html contents of the first matched element. This property is not available on XML documents.
<div><input/></div>
$("div").html();
<input/>
val | String | Set the html contents to the specified value. |
---|
Set the html contents of every matched element. This property is not available on XML documents.
<div><input/></div>
$("div").html("<b>new stuff</b>");
<div><b>new stuff</b></div>
name | String | The name of the attribute to remove. |
---|
Remove an attribute from each of the matched elements.
<input disabled="disabled"/>
$("input").removeAttr("disabled")
<input/>
class | String | One or more CSS classes to add to the elements |
---|
Adds the specified class(es) to each of the set of matched elements.
<p>Hello</p>
$("p").addClass("selected")
[ <p class="selected">Hello</p> ]
<p>Hello</p>
$("p").addClass("selected highlight")
[ <p class="selected highlight">Hello</p> ]
class | String | (optional) One or more CSS classes to remove from the elements |
---|
Removes all or the specified class(es) from the set of matched elements.
<p class="selected">Hello</p>
$("p").removeClass()
[ <p>Hello</p> ]
<p class="selected first">Hello</p>
$("p").removeClass("selected")
[ <p class="first">Hello</p> ]
<p class="highlight selected first">Hello</p>
$("p").removeClass("selected highlight")
[ <p class="first">Hello</p> ]
class | String | A CSS class with which to toggle the elements |
---|
Adds the specified class if it is not present, removes it if it is present.
<p>Hello</p><p class="selected">Hello Again</p>
$("p").toggleClass("selected")
[ <p class="selected">Hello</p>, <p>Hello Again</p> ]