jQuery | data() with Examples (original) (raw)
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.
html `
This will attach data to div elementThis will retrieve the attached data to div element
`
Output:
Just after clicking the run button-

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-

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