Working with DOM Attributes using jQuery

In the previous article we learned about manipulating CSS class using jQuery. In this article, we discuss about dealing with DOM attributes using jQuery. jQuery provides flexible methods to get or set DOM attributes and values of the matched elements, and to get or set property values of the matched elements and also to get or set matched element’s HTML content. We can also remove an attribute or a property using these methods. The following is the summary of jQuery methods we can use to manipulate DOM attributes or properties dynamically to a achieve variety of functionality.

also read:

DOM Attributes using jQuery

The following are the methods jQuery supports to work with DOM Attributes. A working example and output screen capture is provided for each of the methods. The following code snippet reads and sets the attribute values.

<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
	console.log("div id is: " + $("#mainDiv").attr("id"));
	console.log("strong text title is: " + $("strong").attr("title"));

		$("strong").attr("title","UPDATED TITLE");
	<h1>.attr() Demo</h1>
	<div id="mainDiv">
		<strong title="Title for strong text.">
                    This text is inside mainDiv.
		<input type="button" id="updateAttr" value="Update"/>

The following screen describes how we can read element’s attributes.

Once we click on the Update button, the title of the strong text has been updated. Please see the following screen capture for the updated title.



The .attr() method either gets or sets the value of the attribute from the matched elements.

$("strong").attr("title", "Updated Title")


The .removeAttr() method removes the attribute from the matched elements.



The .prop() method gets or sets the value of the property from the matched elements.



The .removeProp() method removes the property from the matched elements.



The .html() method gets or sets HTML content of the matched elements.



The .val() method gets or sets the value of the matched elements.

$("#myTextInput").val(‘New value’)

The DOM manipulation method .attr() is very useful to read or to dynamically set the attributes along with values. Also .prop() method is useful to deal with properties. For instance, using .prop() method we can verify the checked property of checkbox. We use .removeAttr() and .removeProp() methods to remove attributes and properties respectively. The .html() method is flexible in either getting or setting full HTML content of the matched elements. The .html() is particularly useful in setting HTML content from AJAX response. Lastly, the .val() method is used to get or set the value of matched element, for instance we can either read or set the text input value.

also read:

If you have any questions, please post it in the comments section. If you are interested in receiving the future articles on jQuery and Java topics, please subscribe here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!