Problem
The jQuery Quicksand plugin is what I’m using. I need to retrieve the clicked item’s data-id and deliver it to a webservice.
What is the data-id attribute and how do I acquire it? To re-bind the click event for sorted items, I’m using the.on() method.
Asked by Bruce Adams
Solution #1
To get the contents of the attribute data-id (like in link) you have to use
$(this).attr("data-id") // will return the string "123"
or.data() (if you’re using a newer version of jQuery >= 1.4.3)
$(this).data("id") // will return the number 123
data-idNum will not function, however data-idnum will.
Answered by Marcel Jackwerth
Solution #2
We may use the getAttribute and setAttribute methods to retrieve or alter these characteristics using existing, native JavaScript, as seen below:
Through JavaScript
<div id='strawberry-plant' data-fruit='12'></div>
<script>
// 'Getting' data-attributes using getAttribute
var plant = document.getElementById('strawberry-plant');
var fruitCount = plant.getAttribute('data-fruit'); // fruitCount = '12'
// 'Setting' data-attributes using setAttribute
plant.setAttribute('data-fruit','7'); // Pesky birds
</script>
Through jQuery
// Fetching data
var fruitCount = $(this).data('fruit');
OR
// If you updated the value, you will need to use below code to fetch new value
// otherwise above gives the old value which is intially set.
// And also above does not work in ***Firefox***, so use below code to fetch value
var fruitCount = $(this).attr('data-fruit');
// Assigning data
$(this).attr('data-fruit','7');
Read this documentation
Answered by Lalit Kumar Maurya
Solution #3
A word of caution. Keep in mind that if you change the data- property dynamically with JavaScript, the data() jQuery function will not reflect your changes. You must also change it using the data() function.
<a data-id="123">link</a>
JavaScript:
$(this).data("id") // returns 123
$(this).attr("data-id", "321"); //change the attribute
$(this).data("id") // STILL returns 123!!!
$(this).data("id", "321")
$(this).data("id") // NOW we have 321
Answered by Serge Shultz
Solution #4
You can also utilize HTML5 dataset API if you aren’t concerned about ancient Internet Explorer browsers.
HTML
<div id="my-div" data-info="some info here" data-other-info="more info here">My Awesome Div</div>
JavaScript
var myDiv = document.querySelector('#my-div');
myDiv.dataset.info // "some info here"
myDiv.dataset.otherInfo // "more info here"
Demo: http://html5demos.com/dataset
http://caniuse.com/#feat=dataset#feat=dataset#feat=dataset#feat=dataset#feat=dataset#feat=dataset#
Answered by Roni
Solution #5
You can also make use of:
<select id="selectVehicle">
<option value="1" data-year="2011">Mazda</option>
<option value="2" data-year="2015">Honda</option>
<option value="3" data-year="2008">Mercedes</option>
<option value="4" data-year="2005">Toyota</option>
</select>
$("#selectVehicle").change(function () {
alert($(this).find(':selected').data("year"));
});
Here’s an example of a working example: https://jsfiddle.net/ed5axgvk/1/
Answered by curiousBoy
Post is based on https://stackoverflow.com/questions/5309926/how-can-i-get-the-data-id-attribute