Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data attribute becomes integer

Tags:

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".

like image 227
Larsi Avatar asked Apr 24 '12 11:04

Larsi


People also ask

What is a data attribute?

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.

Which tag is data attribute?

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).

How do you set a data attribute?

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.

How get data attribute from Element?

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.


2 Answers

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.

like image 183
binarious Avatar answered Nov 03 '22 01:11

binarious


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

like image 45
ant7 Avatar answered Nov 03 '22 00:11

ant7