SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Javascript variable variables

    Hi there,

    I'm not sure what to search for here, although I'm sure this kind of thing has been covered.

    Basically, I have the following code - which I will explain:
    Code:
        if("pv" + elem.name.substring( 2 ))
        {
          //what the heck do i do here???
        }
    pv1, pv2, pv3... are elements being passed from another javascript function. Basically I just want to grab their values.

    Can anyone tell me how to do this? As well - is there any terminology that I should know for future reference - in case I need to do further searches?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick fix is to use eval():
    Code:
        if(eval("pv" + elem.name.substring( 2 )))
        {
          var newVariable = eval("pv" + elem.name.substring( 2 ));
        }

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    Now I'm getting an undefined error whereas before I was getting a defined variable. I'm wondering if this is happening because I'm not scoping the variable with document.forms[0]...

    My question with regards to this is, how do you write that syntactically:

    document.forms[0]."pv" + elem.name.substring( 2 ) would be incorrect I'm guessing.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright... got what I need!

    So, in order to not just be a leech on the site: here's the solution:

    Code:
    if(document.forms[0].item("pv" + elem.name.substring( 2 )))
        {
            alert(document.forms[0].item("pv" + elem.name.substring( 2 )).value);
        }
    I've done an alert, but you could put whatever you want.

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah ok I thought that pvX was a variable not a form item

    You could also use
    document.forms[0].elements["pv" + elem.name.substring( 2 )]

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jim

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    For variables it is better to use window['pv'+x] rather than eval('pv'+x)

    There is rarely any situation where eval is required or even the best choice.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eval is often touted as the parser for JSON.

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The person who first proposed JSON also developed a parser for it that does NOT require eval and he recommends that eval definitely not be used for converting JSON as there are security issues.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, actually, at json.org they recommend eval unless security is a concern, and then suggest using the parser. The parser does, after all, add packet overhead.

    http://www.json.org/js.html


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
  •