set custom data attribute jquery code example

Example 1: set custom data attribute jquery

//set the custom data value
let mainDiv = $('#yourDivId').data('anyName', value);
//access the custom data value
mainDiv.data('anyName');

Example 2: jquery set data attribute value

//
var a = $('#myElementID').data('myvalue'); //get myvalue $('#myElementID').data('myvalue',38); //set myvalue

Example 3: set data attribute with a string jquery

alert($('#outer').html());   // alerts 
var a = $('#mydiv').data('myval'); //getter $('#mydiv').attr("data-myval","20"); //setter alert($('#outer').html()); //alerts

Tags:

Misc Example