jquery get value by data attribute code example
Example 1: jquery select by data attribute
$('div[data-key=value]');
Example 2: javascript get data-id attribute
//get data-id attribute in plain Javascript
var element = document.getElementById('myDivID');
var dataID = element.getAttribute('data-id');
//get data-id using jQuery
var dataID = $('myDivID').data('data-id');
Example 3: jquery data attribute
data attribute in jquery
For setting attribute data to the element
Html
Assigning data attribute
jquery
$("#assign").data("title","firstparagraph");
Output:
Assigning data attribute
For getting attribute data from the element
Html
Assigning data attribute
jquery
console.log($("#assign").data("title"));
console.log($("#assign").data());
Output:
firstparagraph
{ title: firstparagraph }
Example 4: data id tag
var dataId = $(this).attr("data-id");