PDA

View Full Version : Passing Paramater that contains a period



SashaGirl
05-20-2011, 04:56 AM
Hello,

I am trying to call a javascript function from an onclick event as such:


onclick='saveRecord(1.1.1);'

When I call the function from the onclick event, I get the error:
"missing ) after argument list".

But when I pass this parameter it works:

onclick='saveRecord(1.1);'


I've tried escaping the periods and adding quotes in the first example, but the error remains the same. Any idea how/if I can pass the parameter 1.1.1 to my function?

Appreciate your help!

bluewalrus
05-20-2011, 05:57 AM
I can't explain why this happens but using -'s in place of the .'s will resolve this.

so


1-1-1

in place of


1.1.1

should work. I have some ideas but am not sure, maybe jscheuer1 or someone with JS knowledge can shine some light here? My assumption is that the periods are being read as the regex any character symbol.

jscheuer1
05-20-2011, 06:53 AM
1.1 is a number. 1.1.1 is perhaps an object with a property that is itself an object with a property of its own, but object names are supposed to start with a letter or an underscore, though some browsers don't seem to mind in some situations. But if it is, it's undefined. So maybe it's just a number with trailing garbage. Either way, it's bound to throw an error. I just double checked, it's a number with trailing garbage, "Expected ) after the 1.1 part" - Loosely from IE 9's developer Tools:


SCRIPT1006: Expected ')'
save_rec.htm, line 13 character 58

Now I'm confused about the recommendation:

1-1-1

Which is also a number. It's negative one. And by the assertion that quoting it doesn't help. Quoting works fine in this simplistic demo:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function saveRecord(rec){
alert(rec);
}
</script>
</head>
<body>
<input type="button" value="Test" onclick="saveRecord('1.1.1');">
</body>
</html>

djr33
05-20-2011, 06:58 AM
Note that the quotes must be altered with single quotes and double quotes-- in the first post, single quotes are used for the onclick attribute, so double quotes should be used. In John's example, it's the opposite. Either will work, but not double+double or single+single. That might have been the original problem.

Additionally, you can escape the quotes:

'sin\'gle'
"dou\"ble"


The short answer (as John has already explained) is that it's not a number, and it's not valid syntax in Javascript by itself, so if you want to use that it must be in quotes. If it's not in quotes it will try to parse it as a number if it can or try to parse it as raw Javascript, for example 1-1-1 is equal to 1 minus 1 minus 1 = -1.

SashaGirl
05-22-2011, 03:29 AM
djr33 & jscheuer1 - appreciate the suggestions. I got it to work. My issue was I was doing a single quote in a single quote. Once I changed the inside quotes to double, it worked beautifully! Thanks!!