DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Categories: jQuery

jQuery Data Method

Share

jQuery data method gives you the ability to associate arbitrary data with HTML DOM nodes and JavaScript objects.

As of jQuery 1.4.3, you have the ability to use this method on JavaScript objects and call for element modification.

jQuery.data() method store arbitrary data associated with the specified element or returns the value that was set.

Syntax:

$(selector).data(name);
$(selector).data(name,value);

Example using jQuery Data:

$("#myDiv").data("key","value");

You can pass the object as well:

$("#myDiv").data({"name":"John","age":30});

Above example is same as:

$("#myDiv").data("name","John").data("age",30);

Read Also: Select an element with multiple classes in jQuery

You can get the value by calling it in a single parameter:

var name = $("#myDiv").data("name"); // Returns "John"

Internally, jQuery creates an empty object i.e. $.cache, which is used to store the values that you set via the data method.

Get more details from jQuery documentation.

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

The “Shortage” of Women in Tech Leadership Is Not Real

Ask any conference organizer why their speaker lineup skews heavily male, and the answer is…

1 week ago

8 Great Success Business Stories

In the business world, there’s no such thing as a new idea. The best we…

3 weeks ago

Faceless YouTube vs Blogging: Which Monetization Path Fits Beginners Better?

Both of these work. Both are difficult. And anyone telling you one is obviously better…

3 weeks ago

Spreading the Word of Digital Marketing Physically: A How-To Guide

Digital marketing naturally focuses on online spaces. Businesses invest in social media, SEO, email campaigns,…

3 weeks ago

What Skills Do You Need to Become a Full-Stack Developer? A Complete Roadmap

Introduction There is an increasing need for full-stack developers because companies want people who can…

2 months ago

The Enduring Influence Of Classic Russian Literature On Modern Storytelling

Classic Russian literature holds a quiet power that still shapes stories today. Its voice feels…

2 months ago