get data-id jquery code example

Example 1: jquery get data attribute value

/* html */
<a data-id="123">link</a>

/* js */
$(this).attr("data-id") // returns string "123"

$(this).data("id") // returns number 123 (jQuery >= 1.4.3 only)

Example 2: jquery get element id

$(this).attr('id')

Example 3: jquery get data-id

const dataId = $('#elementId').attr("data-id");

Example 4: get id by this jquery

alert($(this).attr('id'))

Example 5: 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 6: data id tag

var dataId = $(this).attr("data-id");