1

How do I get the correct number from this data url ?

<a href="#" id="number" data-number="106454129446370134989">click me</a>

$(function(){
$('#number').click(function(e){
        e.preventDefault();
        var number= $(this).data('number');
        alert(number);
            //106454129446370130000
})

edit: seems .data() has some strange behaviour. Solved it by adding || around the number and stripping it later.

edit2: problem was with jquery 1.7 branch. 1.9 works :-)

Arjen
  • 416
  • 4
  • 12
  • 3
    Which browser? Works here using chrome: http://jsfiddle.net/GQ83Y/ FYI, depending what you are attempting to do, you shouldn't convert it to number, keep it as string – A. Wolff Nov 25 '13 at 13:46
  • 1
    http://stackoverflow.com/questions/4288821/how-to-deal-with-big-numbers-in-javascript – Johan Nov 25 '13 at 13:49
  • @A.Wolff It's not converted to a number anywhere in the code above… – feeela Nov 25 '13 at 13:52
  • @feeela ya, that's why i told code should work – A. Wolff Nov 25 '13 at 13:53
  • @feeela Maybe `$().data` auto-converts the string to a number, in some browser or version of jQuery? I agree that I couldn't reproduce that behavior, but if that is what's happening, the OP could get around this by adding a non-numeric character to the start or end of the value, and then stripping it after retrieval. – apsillers Nov 25 '13 at 13:55
  • @apsillers ya, seems fair but then that means .data() behaviour differs from browser to browser as .data() doesn't convert string to number in chrome, as i can test it – A. Wolff Nov 25 '13 at 13:59
  • @Johan, that doesnt work since the first string I get is allready different. Im dont receive the correct number so I cant do anything with it :-) – Arjen Nov 25 '13 at 14:02
  • chrome,latest. Solved it by adding |123456....0| and stripping it later :-) Thx for the suggestion !! – Arjen Nov 25 '13 at 14:03
  • @user1416256 try using: `$(this).attr('data-number');` should fix string beeing converted – A. Wolff Nov 25 '13 at 14:03
  • @A.Wolff, thx ! problem btw was with jquery 1.7 branch. – Arjen Nov 25 '13 at 14:07
  • Oh my bad, didn't notice that the numbers differed. – Johan Nov 25 '13 at 14:12
  • http://jsfiddle.net/GQ83Y/2/ – Johan Nov 25 '13 at 14:12

1 Answers1

1

Use this

$(this).attr('data-number') 

Bcoz

$(this).data('number') 

is returning number not string

Murali Mopuru
  • 6,086
  • 5
  • 33
  • 51