How to Get the data-id Attribute of html Element Using jQuery?
Data-id attribute
you can simply use attr() method to find the data-id attribute of an HTML element.
File Name :
you can get the contents of the attribute data-id you have to use
<a href="" id="7" data-id="786">Get data id attribute </a>
<script>
$(this).attr("data-id") // will return the string "786"
############## OR ################
$(this).data("id") // will return the number 786
</script>
Note :- after data- must be lowercase, e.g. data-idNumber will not work, but data-idnumber will work.
File Name :
<a data-id="786">link</a>
<script>
$(this).data("id") // returns 786
$(this).attr("data-id", "687"); //change the attribute
$(this).data("id") // STILL returns 123!!!
$(this).data("id", "687")
$(this).data("id") // NOW we have 687
</script>
File Name :
<div id="my-div" data-name="mahira" data-nickname="sana">Get Name and nick name</div>
<script>
var myDiv = document.querySelector('#my-div');