ItSolutionStuff.com

How to Set and Get Custom Attribute Value using JQuery?

By Hardik Savani • November 5, 2023
jQuery

In this example, we will learn how to get custom attribute value in jquery and how to set custom attribute value in jquery. we will use attr() and data() for getting custom attribute value and set custom attribute value in js.

We can easily set data attribute value in jquery, also you can do it same thing with custom attribute value in jquery.

We almost require to set and get custom attribute when you are working with jquery code on your php or laravel project. when ever you need to set and get custom attribute value then this examples will help you.

I gave you bellow two example, so you can use any one as you want.

Example 1

<!DOCTYPE html>

<html>

<head>

<title>Jquery Set custom data attribute value | Jquery Get custom data attribute value - ItSolutionStuff.com</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

</head>

<body>

<div id="example1">Working with me</div>

<button class="set-attr">Set Attribute</button>

<button class="get-attr">Get Attribute</button>

<script type="text/javascript">

$(".set-attr").click(function(){

$("#example1").attr('custom-name', 'ItSolutionStuff.com');

});

$(".get-attr").click(function(){

var name = $("#example1").attr('custom-name');

alert(name);

});

</script>

</body>

</html>

Example 2

<!DOCTYPE html>

<html>

<head>

<title>Jquery Set custom data attribute value | Jquery Get custom data attribute value - ItSolutionStuff.com</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

</head>

<body>

<div id="example2">Working with me</div>

<button class="set-data">Set Attribute</button>

<button class="get-data">Get Attribute</button>

<script type="text/javascript">

$(".set-data").click(function(){

$("#example2").data('name', 'ItSolutionStuff.com 2');

});

$(".get-data").click(function(){

var name = $("#example2").data('name');

alert(name);

});

</script>

</body>

</html>

I hope it can help you...

Tags: jQuery
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Remove Key Value from Array using JQuery?

Read Now →

How to Get Selected Radio Button Value in JQuery?

Read Now →

JQuery Redirect to Another Page After 5 Seconds Example

Read Now →

How to Get Last Element from Array using JQuery?

Read Now →

JQuery Check If Value Exists or Not in Array Example

Read Now →

How to Split String to Array By Comma using JQuery?

Read Now →

How to Remove Empty or Null Values from JSON using JQuery?

Read Now →

How to Check If Key Exists in JSON Object using JQuery?

Read Now →

How to Disable F5 Refresh Button using JQuery?

Read Now →

JQuery Ajax CRUD Operations in PHP MySQL Example

Read Now →

How to Get Gravatar Image from Email using JQuery?

Read Now →

How to Remove Query String from URL using JQuery?

Read Now →

Check and Uncheck All Checkbox using JQuery Example

Read Now →

How to access PHP variables in JQuery?

Read Now →