Unable to set data attribute using jQuery Data() API

It is mentioned in the .data() documentation The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery) This was also covered on Why don’t changes to jQuery $.fn.data() update the corresponding html 5 data-* … Read more

How to use dashes in HTML-5 data-* attributes in ASP.NET MVC

This problem has been addressed in ASP.Net MVC 3. They now automatically convert underscores in html attribute properties to dashes. They got lucky on this one, as underscores are not legal in html attributes, so MVC can confidently imply that you’d like a dash when you use an underscore. For example: @Html.TextBoxFor(vm => vm.City, new … Read more

How to set data attributes in HTML elements

HTML <div id=”mydiv” data-myval=”10″></div> JS var a = $(‘#mydiv’).data(‘myval’); //getter $(‘#mydiv’).data(‘myval’,20); //setter Demo Reference From the reference: jQuery itself uses the .data() method to save information under the names ‘events’ and ‘handle’, and also reserves any data name starting with an underscore (‘_’) for internal use. It should be noted that jQuery’s data() doesn’t change … Read more

jQuery selectors on custom data attributes using HTML5

$(“ul[data-group=’Companies’] li[data-company=’Microsoft’]”) //Get all elements with data-company=”Microsoft” below “Companies” $(“ul[data-group=’Companies’] li:not([data-company=’Microsoft’])”) //get all elements with data-company!=”Microsoft” below “Companies” Look in to jQuery Selectors :contains is a selector here is info on the :contains selector

How can I get the values of data attributes in JavaScript code?

You need to access the dataset property: document.getElementById(“the-span”).addEventListener(“click”, function() { var json = JSON.stringify({ id: parseInt(this.dataset.typeid), subject: this.dataset.type, points: parseInt(this.dataset.points), user: “Luïs” }); }); Result: // json would equal: { “id”: 123, “subject”: “topic”, “points”: -1, “user”: “Luïs” }