Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with jQuery data() treating string as number

I have a MySQL BIGINT that I am storing in HTML5 data. Then I'm trying to access that value and pass it through an AJAX call.

<div data-id="211285677671858177"> 

And the JavaScript:

var send_data = {     id: '' + $(this).data('id') } $.post('/send.php', send_data); 

The issue is that the jQuery data function seems to retrieve that value as a floating point and not a string. So appending it to a blank string isn't helping because it's already too late - it's already been rounded (in this case to 211285677671858180). What can I do to fix this?

like image 641
andrewtweber Avatar asked Jun 09 '12 03:06

andrewtweber


1 Answers

This isn't a case of "long int" really, the number you're getting is the closest available representation as a floating-point number.

Anyway, you want the value as a string. Quote the jQuery docs for .data (emphasis mine):

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 50
John Flatness Avatar answered Sep 21 '22 13:09

John Flatness