I'm using the jQuery Quicksand plugin. I need to get the data-id of the clicked item and pass it to a webservice.
How do I get the data-id attribute? I'm using the .on() method to re-bind the click event for sorted items.
$("#list li").on('click', function() { // ret = DetailsView.GetProject($(this).attr("#data-id"), OnComplete, OnTimeOut, OnError); alert($(this).attr("#data-id")); });<script src=""></script> <ul> <li> <a href="#"> <img src="themes/clean/images/win.jpg" alt="get data-id" /> </a> </li> </ul>416 Answers
To get the contents of the attribute data-id (like in <a>link</a>) you have to use
$(this).attr("data-id") // will return the string "123" or .data() (if you use newer jQuery >= 1.4.3)
$(this).data("id") // will return the number 123 and the part after data- must be lowercase, e.g. data-idNum will not work, but data-idnum will.
If we want to retrieve or update these attributes using existing, native JavaScript, then we can do so using the getAttribute and setAttribute methods as shown 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'); 0Important note. Keep in mind, that if you adjust the data- attribute dynamically via JavaScript it will not be reflected in the data() jQuery function. You have to adjust it via data() function as well.
<a>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 0If you are not concerned about old Internet Explorer browsers, you can also use HTML5 dataset API.
HTML
<div>My Awesome Div</div> JavaScript
var myDiv = document.querySelector('#my-div'); myDiv.dataset.info // "some info here" myDiv.dataset.otherInfo // "more info here" 1You can also use:
<select> <option value="1">Mazda</option> <option value="2">Honda</option> <option value="3">Mercedes</option> <option value="4">Toyota</option> </select> $("#selectVehicle").change(function () { alert($(this).find(':selected').data("year")); }); 0This piece of code will return the value of the data attributes. For example: data-id, data-time, data-name, etc.
I have shown it for the id:
<a href="#">Click</a> JavaScript: Get the value of the data-id -> a1
$(this).data("id"); JQuery: This will change the data-id -> a2
$(this).data("id", "a2"); JQuery: Get the value of the data-id -> a2
$(this).data("id"); 0HTML
<span>test</span> JavaScript
$(this).data().value; or
$("span#spanTest").data().value; ANS: 50
Accessing the data attribute with its own id is a bit easy for me.
$("#Id").data("attribute"); function myFunction(){ alert($("#button1").data("sample-id")); }<script src=""></script> <button type="button" onclick="myFunction()"> Clickhere </button>0var id = $(this).dataset.id works for me!
0I use $.data:
//Set value 7 to data-id $.data(this, 'id', 7); //Get value from data-id alert( $(this).data("id") ); // => outputs 7 Using jQuery:
$(".myClass").load(function() { var myId = $(this).data("id"); $('.myClass').attr('id', myId); }); Try
this.dataset.id $("#list li").on('click', function() { alert( this.dataset.id ); });<script src=""></script> <ul> <li> <a href="#"> <img src="themes/clean/images/win.jpg" alt="get data-id >>CLICK ME<<" /> </a> </li> </ul>The issue is you are not specifying the option or selected option of dropdown or list, Here is an example for dropdown, i am assuming a data attribute data-record.
$('#select').on('change', function(){ let element = $("#visiabletoID"); element.val($(this).find(':selected').data('record')); }); for pure js
let btn = document.querySelector('.your-btn-class'); btn.addEventListener('click',function(){ console.log(this.getAttribute('data-id')); }) 1For those looking to dynamically remove and re-enable the tooltip, you can use the dispose and enable methods. See .tooltip('dispose').
I have a span. I want to take the value of attribute data-txt-lang, which is used defined.
$(document).ready(function () { <span>EN</span> alert($('.txt-lang-btn').attr('data-txt-lang')); }); 0