SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I use a value passed into a javascript function as a "literal"?

    How do I use a value passed into a javascript function as a "literal"?

    For example, let's say I have the following in my form:
    Code:
    <form name="form1">
    	<input type="hidden" name="registrant_firstname" value="First Name Goes Here">
    <input type="hidden" name="registrant_lastname" value="Last Name Goes Here">
    	<input type="text" name="admin_firstname" value="">
    	<input type="button" name="nothing" value="Click here to get the firstname" onClick="doName("registrant");">
    </form>
    The bolded part in the javascript function below is what I don't know how to do.. I want to take the incoming parameter and use it in the indicated spot as a "literal". (Not sure if "literal" is the right term.) So var1 would be replaced with the string of "registrant".

    Code:
    <script language="javascript">
    
    doName(var1) {
    
    	document.form1.admin_firstname.value = document.form1.var1_firstname.value
    
    	// So the second part above would actually become "document.form1.registrant_firstname.value".
    }
    
    </script>
    So when the user clicks the button in the form, the admin_firstname value in the textbox would get set to the value of the hidden registrant_firstname field.

    Is this possible? If so, can someone show me how to do it?

    Thanks!
    Last edited by hstraf; Apr 10, 2002 at 19:38.
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    <script language="javascript">
    
    doName(var1) {
    
      var sName;
      sName = var1 + "_firstname";
    
      document.form1.admin_firstname.value = document.all(sName).value;
    
    }
    
    </script>

  3. #3
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    There are really two ways to do this for cross-browser:

    frmV1 = eval("document.form1."+var1+"_firstname");
    // or:
    // frmV1 = document.form1.elements[var1+"_firstname"];

    document.form1.admin_firstname.value = frmV1.value

    aDog


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
  •