Check out this simple example on jsfiddle
<div id ="a" data-siteid="00005">00005 turns into:</div> <div id="b" data-siteid="S00005">S00005 turns into: </div>
code
$('#a').append($('#a').data("siteid")); $('#b').append($('#b').data("siteid"));
result
00005 turns into:5 S00005 turns into: S00005
I would like to return "00005" and "S00005".
In short, a data attribute is a single-value descriptor for a data point or data object. It exists most often as a column in a data table, but can also refer to special formatting or functionality for objects in programming languages such as Python.
HTML data-* Attribute The data-* attribute gives us the ability to embed custom data attributes on all HTML elements. The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).
Adding a data attribute is easy. Any HTML element can have any number of data attributes added to its opening tag. We simply type data- followed by the name of our attribute into the element's opening tag alongside any other attributes we're already using.
Approach: First, select the element which is having data attributes. We can either use the dataset property to get access to the data attributes or use the . getAttribute() method to select them by specifically typing their names.
Try
$('#a').append($('#a').attr('data-siteid')); $('#b').append($('#b').attr('data-siteid'));
From the jQuery Docs
Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.
Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.
From here: http://api.jquery.com/data/#data-html5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With