SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trouble passing form name to function

    I have 2 forms(form1,form2) on one page. I'm trying to re-use a function for both forms. The function is a simple if statement matching against one of the fields that both forms have.

    right now I'm passing it like so... function('form1')

    Code:
    function(txt) {
    var="document.forms."+txt+".field.value"
    if(var=="ant1832"){...
    I check what was passed to the variable using alert(var) and its "document.forms.form1.value."

    How can I pass the actual value to the variable?
    Anthony

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, what you're trying to do is evaluate a string as code, which can be done using the eval function

    var value = eval( "document.forms." + txt + ".field.value" );

    However, I don't ever recommend using eval. Fortunately, javascript and the DOM provide for us, a better way to achieve the above statement. This is done using the bracket-syntax, which allows us to assert properties as strings, as opposed to the more common dot-syntax.

    var value = document.forms[txt].field.value;

    Make sure to declare a valid variable name to store your data, as well.

    Happy Coding [img]images/smilies/biggrin.gif[/img]
    Last edited by beetle; Nov 3, 2003 at 13:37.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    exactly what I needed. I knew it would be something really easy. Thanks!
    Anthony


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
  •