#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    456
    Rep Power
    15

    javascript variables and object names


    Here is my simple JavaScript:

    function xoff(xname,checkname,linenum) {
    document[xname].src = 'xiconoff.gif'
    document[checkname].src = 'checkicon.gif'
    document.auctionlines.linenum.value = 'check'
    }

    I call it with something like:

    xoff('1x','1c','a453465')

    The problem is that with the above function call, JavaScript thinks the object (a form text box) is named:

    document.auctionlines.linenum.value

    when I want it to be named:

    document.auctionlines.a453465.value

    It doesn't realize that 'linenum' is a variable, and actually includes 'linenum', not the value of this variable, which should be 'a453465'.

    Any ideas on how I get this to work?

    THANKS!
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    You need to look at the eval function, for example:

    <html>
    <body>
    <script type='text/javascript'>
    function changeMe(linenum) {
    eval("document.forms[0]." + linenum + ".value = 'help'")
    }
    </script>
    <form>
    <input type='text' onChange='changeMe(this.name)' name='text1'>
    </form>
    </body>
    </html>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    456
    Rep Power
    15
    Just what I was looking for. I'm not much of a JavaScript writer. I normally just cut and paste from other scripts to make my own. That 'eval' function is a help one to know.

    Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo