1<a data-id="123">link</a>
2
3
4var id = $(this).data("id"); // Will set id to 123
1$( "body" ).data( "foo", 52 );
2$( "body" ).data( "bar", { isManual: true } );
3$( "body" ).data( { baz: [ 1, 2, 3 ] } );
4$( "body" ).data( "foo" ); // 52
5$( "body" ).data(); // { foo: 52, bar: { isManual: true }, baz: [ 1, 2, 3 ] }
6
1data attribute in jquery
2
3For setting attribute data to the element
4Html
5<p id="assign">Assigning data attribute</p>
6jquery
7$("#assign").data("title","firstparagraph");
8
9Output:
10<p id="assign" data-title="firstparagraph">Assigning data attribute</p>
11
12For getting attribute data from the element
13Html
14<p id="assign" data-title="firstparagraph">Assigning data attribute</p>
15jquery
16console.log($("#assign").data("title"));
17console.log($("#assign").data());
18
19Output:
20firstparagraph
21{ title: firstparagraph }