SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast cilverphox's Avatar
    Join Date
    Jan 2005
    Location
    Petoskey, MI
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reading form name as array

    I have a form with several radio buttons having the same name, but different values. (please realize that the following in not functional code, but rather, conceptual).

    Code:
    <input type="radio" name="ID" value="Dan">Dan
    <input type="radio" name="ID" value="Joe">Joe
    <input type="radio" name="ID" value="Steve">Steve
    
    <input type="text" name="Name" value="this.form.ID.value">
    Upon submitting the form, the value of the selected element is to be inserted into an element of another name. For example, when "Joe" is selected, then element "Name" has a value of Joe. This is fairly simple logic and I was able to figure it out...well, sort of. It work ONLY if there is one radio button, but does NOT work if there are multiple. This makes sense because element ID logically becomes an array when multiple elements with the same name are present. My question is: Does element ID become an array and if so, how can I determine with ID was selected.

    -Cilverphox

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should get you started:
    Code:
    <form name="myForm">
    	<input type="radio" name="ID" value="Dan">Dan
    	<input type="radio" name="ID" value="Joe" checked>Joe
    	<input type="radio" name="ID" value="Steve">Steve
    	
    	<input type="text" name="Name" value="">
    </form>
    
    <script type="text/javascript">
    var oForm = document.forms["myForm"];
    var aInputs = oForm["ID"];
    var oName = oForm["Name"];
    
    for(var i=0; i<aInputs.length; i++)
    {
    	if(aInputs[i].checked)
    	{
    		oName.value = aInputs[i].value;
    	}
    }
    </script>


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
  •