3

I have a javascript function that contains an argument.

<a href="#" onclick="changeAvatar(0684839741);">Click here</a>

I need that argument to be a string because sometimes there will be Letters or a Leading 0 that I will need to preserve like in this example.

I checked to make sure that the argument that is passed in is a string with

x.constructor === String

But in my changeAvatar function, the argument comes through as no longer a string. Instead it is a number and removes the leading zero or breaks if it contains a letter.

How do I get the argument to retain it's type?

asked Dec 21, 2011 at 0:12
1
  • 1
    Have you tried adding single quote marks (apostrophes) around your argument? Commented Dec 21, 2011 at 0:14

2 Answers 2

9

You've typed it as a javascript number as this line of javascript evaluates it to a number:

changeAvatar(0684839741);

If you want it to be a string, you have to do it this way where you explicitly declare it as a string like:

changeAvatar('0684839741');

which means the whole line should be this:

<a href="#" onclick="changeAvatar('0684839741');">Click here</a>
answered Dec 21, 2011 at 0:14
Sign up to request clarification or add additional context in comments.

Comments

2

Add single quotes:

<a href="#" onclick="changeAvatar('0684839741');">Click here</a>
answered Dec 21, 2011 at 0:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.