VOOZH about

URL: https://www.geeksforgeeks.org/jquery/jquery-data-with-examples/

⇱ jQuery | data() with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

jQuery | data() with Examples

Last Updated : 27 Oct, 2021

The data() is an inbuilt method in jQuery which is used to attach data or get data for the selected elements. 
Syntax: 

$(selector).data(para1);


Parameter : It accepts an optional parameter "para1" which specifies the name of the data to retrieve for the selected element. 
Return Value: It returns the retrieved data for the selected element.
jQuery code to show the working of data() method: 
Code #1: 
In the below code, data is attach to the selected element.  

Output: 
 

👁 Image


Code #2: 
In the below code, The data is attaching and retrieving from the "div" element using buttons. 

Output: 
Just after clicking the run button- 
 

👁 Image


After clicking the "This will retrieve the attached data to div element" button just after clicking the "This will attach data to div element" button- 
 

👁 Image


After clicking the "This will retrieve the attached data to div element" button without clicking the "This will attach data to div element" button- 
 

👁 Image


 

Comment
Article Tags:
Article Tags:

Explore