How to Get Attribute Value in JQuery?

If you’re a beginner and want to obtain attribute value, you can do so with ease. We might occasionally also need to obtain custom attribute values, namely data attribute values. I’ll show you three ways to retrieve a custom attribute’s value in jQuery in the example that follows.

Example:

<html lang="en">
<head>
	<title>Get Custom Attribute Value</title>
	<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.3/jquery.min.js"></script>
</head>
<body>
	<a href="example.com" data-id="1" myclass="hd">Example</a>
    <script type="text/javascript">
    	

	    var a_href = $("a").attr("href");
	    alert(a_href);
	    

	    var a_data_id = $("a").data("id");
	    alert(a_data_id);
	    

	    var a_myclass = $("a").attr("myclass");
	    alert(a_myclass);
	    

    </script>
</body>
</html>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x