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.
<img src="test.jpg"/>
$("img").attr("src");
test.jpg
prop | Hash | A set of key/value pairs to set as object properties. |
---|
Set a hash of key/value object properties to all matched elements. This serves as the best way to set a large number of properties on all matched elements.
<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.
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.
<img/>
$("img").attr("src","test.jpg");
<img src="test.jpg"/>
Retrieve 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.
<p>Test Paragraph.</p>
$("p").text();
Test Paragraph.
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 | A CSS class to add to the elements |
---|
Adds the specified class to each of the set of matched elements.
<p>Hello</p>
$("p").addClass("selected")
[ <p class="selected">Hello</p> ]
class | String | A CSS class to remove from the elements |
---|
Removes the specified class from the set of matched elements.
<p class="selected">Hello</p>
$("p").removeClass("selected")
[ <p>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> ]