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 −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<div id="test" data-details='["My", "Name", "is", "John"]'></div>
   var value = $('#test').data('details');

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.


This will produce the following output −

Updated on: 01-Sep-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started