javascriptstringinteger

Passing a string with hyphens gets evaluated as an integer (subtractions!) js


I am trying to pass a value like "108-17-014" to a function through onClick...

hyphenatedId = "107-17-14"
dialogBody += " <a href='javascript:void(0);' onClick='class.exampleFunction("+ hyphenatedId +");'>link title</a>"; 

And inside class.exampleFunction,

exampleFunction : function ( hyphenatedId ) {
    console.log(hyphenatedId);
}

However, when I click the link, the console.log does not show "107-17-14" but instead shows 76 ... because it is interpreting the hyphens as subtraction operators.

How do I prevent this from happening? Any other suggestions are welcome =)


Solution

  • Pass as string

    dialogBody += " <a href='javascript:void(0);' onClick='class.exampleFunction(\""+ hyphenatedId +"\");'>link title</a>";