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

12
  • 3
    Which browser? Works here using chrome: jsfiddle.net/GQ83Y FYI, depending what you are attempting to do, you shouldn't convert it to number, keep it as string Commented Nov 25, 2013 at 13:46
  • 1
    stackoverflow.com/questions/4288821/… Commented Nov 25, 2013 at 13:49
  • @A.Wolff It's not converted to a number anywhere in the code above… Commented Nov 25, 2013 at 13:52
  • @feeela ya, that's why i told code should work Commented Nov 25, 2013 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. Commented Nov 25, 2013 at 13:55

1 Answer 1

1

Use this

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

Bcoz

$(this).data('number') 

is returning number not string

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.