![]() |
VOOZH | about |
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:
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-
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-