Jquery Get Attribute Value Example

11-Apr-2023

.

Admin

Hi Guys,

In this example,i will learn you how to get attribute value in jquery.you can easy and simply get the attribute value in jquery.

Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.In following example i give you three way to get custom attribute value in jquery.

Example 1:


Syntax :

When a single parameter is passed to the .attr() function it returns the value of passed attribute on the selected element.

$([selector]).attr([attribute name]);

<html lang="en">

<head>

<title>Get Attribute Value</title>

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</head>

<body>

<input type="text" name="name" id="textbox1">

<script type="text/javascript">

var input_type = $("input").attr("type");

alert(input_type);

var input_id = $("input").data("id");

alert(input_id);

var input_name = $("input").attr("name");

alert(input_name);

</script>

</body>

</html>

Example 2 :

jQuery offers .data() function in order to deal with data attributes. .data() function returns the value of the data attribute on the selected element.

Syntax :

$([selector]).data([attribute name]);

<html lang="en">

<head>

<title>Get Attribute Value</title>

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</head>

<body>

<article data-row="4"></article>

<script type="text/javascript">

var article_data_row = $("article").data("row");

alert(article_data_row);

</script>

</body>

</html>

It will help you...

#Jquery