SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript pass value from select box to input box

    Hi

    I have a html/php web page and I have 2 Forms on it.

    Form2 contains a select box and I was able to get the value from that just fine.

    Form1 contains a php list of all users in a table.
    I have a hidden field that I want to put the value of the select box in Form2 (

    <form name="Form2">
    <select name="sortme" id="sortme" onchange="getValue(this);document.Form1.sortme2.value=this.value;">
    </form>

    and pass that value to a hidden field I have in Form1 (on same page).

    <form name="Form1">
    <input type='text' value='' name='sortme2' id='kath'>
    </form>

    How do I do this?

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First you need to abandon the habit of addressing forms by the name; the attribute is deprecated except for the elements of forms.

    The neatest way might be to use a function to address the desired element, by passing it the ID (not name) of its form, the name of the element and the value:

    Code:
    <form id='f1'>
      <input name='inp1' type=text>
    </form>
    
    <script type='text/javascript'>
    
    function writeFormElem( formID, elemName, elemValue )
    {
      var formRef = document.getElementById( formID );
    
      if( formRef && formRef.nodeName == 'FORM' && formRef.id == formID )
        formRef[ elemName ].value = elemValue;
      else
        alert( 'Invalid form ID:"' + formID + '"' );   
    }
    
    writeFormElem( 'f1', 'inp1', 'OK' );
    
    </script>
    Tab-indentation is a crime against humanity.


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
  •