Results 1 to 5 of 5

Thread: Passing Paramater that contains a period

  1. #1
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing Paramater that contains a period

    Hello,

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

    Code:
    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:
    Code:
    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!

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.
    Last edited by bluewalrus; 05-20-2011 at 06:14 AM.
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <!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>
    Last edited by jscheuer1; 05-20-2011 at 07:07 AM. Reason: add detail
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •