Skip to content
Advertisement

Prevent truncating of preceeding zeros in Javascript

I am trying to pass variable to a JavaScript function that then makes an Ajax request using the variable. My problem is that if the value of the variable starts with zeros, the zeros are truncated. For example, if the value is 00056, what is sent for an Ajax call is 56

Here is what I am doing

 <a class="btn" onclick="return callFxn(<?php echo  $var; ?>);" href="" >Click here</a>

How do I adjust this function so that the zeros are not truncated. I guess the variable needs to be sent as a string. I can’t figure out how to do this.

function callFxn(output) {
    alert(output) //value is truncated here
 }

Advertisement

Answer

Put quotes around the argument in the JavaScript to make it a string instead of a number.

<a class="btn" onclick="return callFxn('<?php echo  $var; ?>');" href="" >Click here</a>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement