1

when i call function and give it 154911812952895_154922162951860 it converts the paramter to 1.5491181295289516e+29. this is my code:

<span onclick="javascript:remove_attachment(154911812952895_154922162951860)">&times;</span>
function remove_attachment(id){
    console.log(id);
    console.log(typeof id);
};

the output:

1.5491181295289516e+29
number

i want to return 154911812952895_154922162951860 not 1.5491181295289516e+29

function remove_attachment(id){
    console.log(id);
    console.log(typeof id);
};
<span onclick="javascript:remove_attachment(154911812952895_154922162951860)">&times; CLICK ME</span>

3
  • 2
    It seams to be working fine on JsFiddle. Maybe you could add more informations on your HTML page, how you declare that first variable, Commented Sep 16, 2020 at 15:25
  • 2
    I have added a code snippet to your question. Plz edit it to provide minimal reproducible example. Currently your code works as expected. Commented Sep 16, 2020 at 15:26
  • sorry i edit in Question Commented Sep 16, 2020 at 15:32

3 Answers 3

2

The function argument needs single quotes around the id for it to be considered a string. Without quotes, it will be a number, with double quotes it will error because you are not escaping the onclick event handler which also has double quotes.

function remove_attachment(id){
    console.log(id);
    console.log(typeof id);
};
<span onclick="javascript:remove_attachment('154911812952895_154922162951860')">&times; click</span>

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

Comments

0

I think your error is probably somewhere in your variable or how you pass the value to the function.

this:

<span onclick="javascript:remove_attachment("154911812952895_154922162951860")">&times;</span>

can't work because you are using " to wrap the command and you wrap again the parameter in ". If you run it like this, you get an error.

What works is:

 <span onclick="javascript:remove_attachment('154911812952895_154922162951860')">&times;</span>

or

<span onclick="javascript:remove_attachment(154911812952895_154922162951860)">&times;</span>

The first option will give you a string output (exactly what you want). The second option gives you the number you don't want.

Comments

-1

You're the "victim" of a future feature called Numeric Separators (which is at the moment a stage 4 proposal).

This feature enables developers to make their numeric literals more readable by creating a visual separation between groups of digits. Large numeric literals are difficult for the human eye to parse quickly, especially when there are long digit repetitions.
...
Using underscores (_, U+005F) as separators helps improve readability for numeric literals, both integers and floating-point (and in JS, it's all floating-point anyway):

1_000_000_000           // Ah, so a billion
101_475_938.38          // And this is hundreds of millions

let fee = 123_00;       // $123 (12300 cents, apparently)
let fee = 12_300;       // $12,300 (woah, that fee!)
let amount = 12345_00;  // 12,345 (1234500 cents, apparently)
let amount = 123_4500;  // 123.45 (4-fixed financial)
let amount = 1_234_500; // 1,234,500

To make your remove_attachment() function work you have to wrap the number in (single) quotes to make it an actual string:

<span onclick="javascript:remove_attachment('154911812952895_154922162951860')">&times;</span>

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.