www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Using a variable to reference a form name

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    resolved [RESOLVED] Using a variable to reference a form name

    Hello,

    I have a feeling this is a simple question, but I just can't get it to work. I have a page with many forms updatenotes_1137", "updatenotes_1138", "updatenotes_1139", and so on. Each of these forms have a text input named "b_user." I want to call a JS function with the name of the form and have it change the value of "b_user" from that field.

    Here's a summary of my attempt (this is the exact code I'm working with while trying to figure it out):

    Code:
    <html>
    <head>
    <script>	
    function add_B_User(form_Name)
    {
    	//document.["form_Name"].b_user.value = "Hello World!";
    	alert(form_Name);
    }
    </script>
    </head>
    
    <body>
    
    <form id='updatenotes_1139' action=index.php method=POST>
    <input size=20 type=input name='b_user' value="LVS BOOKS">
    <input type=submit>
    </form>
    
    <script>
    add_B_User("updatenotes_1139");
    </script>
    
    </body>
    </html>


    Now, if i only use the alert:
    Code:
    function add_B_User(form_Name)
    {
    	//document.["form_Name"].b_user.value = "Hello World!";
    	alert(form_Name);
    }
    The alert works just fine, transmitting the input name. So it's gotta be something in the syntax of that the "Hello world!" line of code. Just not sure what .

    Thanks for any help!
    - Alex

  2. #2
    Join Date
    Jun 2013
    Posts
    3
    As a side note, the function also works when I hardcode in the form name:
    Code:
    function add_B_User(form_Name)
    {
    	document.forms.updatenotes_1139.b_user.value = "Hello World!";
    }
    </script>
    Thanks!

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Facepalm. I'm not used to Javascript, but even I should have realized after this many hours that I was using id-based functions while only labeling the actual inputs with "name" and not "id." Ugh. Well, thanks anyhow all .

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles