- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Store and retrieve arrays into and from HTML5 data attributes with jQuery?
To store and retrieve arrays into and from data attributes, use the data() method in jQuery. Following is the syntax −
var anyVariableName= $('#yourIdName).data('yourJavscriptArrayName');
Following is the jQuery code −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale= 1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <div id="test" data-details='["My", "Name", "is", "John"]'></div> <script> var value = $('#test').data('details'); alert(value); </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
- Related Articles
- How to retrieve data from JSON file using jQuery and Ajax?
- How to use jQuery selectors on custom data attributes using HTML5?
- How to store and retrieve date into Sqlite3 database using Python?
- How to store and retrieve a date into MySQL database using Python?
- Creating custom attributes with HTML5
- How to add and remove HTML attributes with jQuery?
- Attributes and usage of element in HTML5
- Attributes and usage of element in HTML5
- How to retrieve files and folders attributes using PowerShell?
- Using HTML5 file uploads with AJAX and jQuery
- How to store data in the browser with the HTML5 localStorage API?
- HTML5 Microdata Attributes
- Escaping/encoding single quotes in JSON encoded HTML5 data attributes
- WebSockets Attributes in HTML5
- How to store and reproduce jQuery events?

Advertisements