Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    41
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Post Textbox value from option name or id

    Hey guys, I recently found a small code in internet which use option value in select tag to insert value in text box, so text box has the exact same value as selected option:

    Code:
    <form name="theform" onsubmit="CheckForm()">
    
    <select name="myOptions" onchange="document.theform.showValue.value=this.value">
    
    
    <option name="(some dynamic text)" id="(some dynamic text)" value="I am the first option">I am the first option</option>
    <option name="(some dynamic text)" id="(some dynamic text)" value="I am the second option">I am the second option</option>
    <option name="(some dynamic text)" id="(some dynamic text)" value="I am the third option">I am the third option</option>
    <option name="(some dynamic text)" id=""(some dynamic text)" value="I am the third option">I am the fourth third option</option>
    
    </select>
    <input type="text" name="showValue"><br>
    </form>
    Now what I want is simple and a little different from this one, I want the text box value to be as exact as selected option name or id (doesn't matter which one) which means if an option selected, text box value has to follow that option name or id.

    Thanks folks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    That is not the right way to go about it! Names and ids do not contain text.

    Code:
    <form name="theform" onsubmit="CheckForm()">
    
    <select name="myOptions" onchange="showBox()">
    <option>I am the first option</option>
    <option>I am the second option</option>
    <option>I am the third option</option>
    <option>I am the fourth option</option>
    
    </select>
    <input type="text" name="showValue" value = "Some Dynamic Text One" size = "50"><br>
    </form>
    
    <script type = "text/javascript">
    function showBox() {
    var texts = [];
    texts[0] = "Some Dynamic Text One";
    texts[1] = "Some Dynamic Text Two";
    texts[2] = "Some Dynamic Text Three";
    texts[3] = "Some Dynamic Text Four";
    var x = theform.myOptions.selectedIndex;
    document.theform.showValue.value = texts[x];
    }
    </script>

    The Pakistanis have a natural ability to commit suicide - BBC Radio cricket commentator
    Last edited by Philip M; 09-04-2011 at 05:24 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    41
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    That is not the right way to go about it! Names and ids do not contain text.

    Code:
    <form name="theform" onsubmit="CheckForm()">
    
    <select name="myOptions" onchange="showBox()">
    <option>I am the first option</option>
    <option>I am the second option</option>
    <option>I am the third option</option>
    <option>I am the fourth option</option>
    
    </select>
    <input type="text" name="showValue" value = "Some Dynamic Text One" size = "50"><br>
    </form>
    
    <script type = "text/javascript">
    function showBox() {
    var texts = [];
    texts[0] = "Some Dynamic Text One";
    texts[1] = "Some Dynamic Text Two";
    texts[2] = "Some Dynamic Text Three";
    texts[3] = "Some Dynamic Text Four";
    var x = theform.myOptions.selectedIndex;
    document.theform.showValue.value = texts[x];
    }
    </script>

    The Pakistanis have a natural ability to commit suicide - BBC Radio cricket commentator
    Thanks Philip but that's not what i really meant, you see the options are dynamic (connected to db and table) and they'll be repeated so we dont really have four options it could be 50 options or 75 options (based on what admin added) anyway the select tag and its repeating options value contain int (number) in db and that text field has to contain a title for that int so i cant get options value as text box one because it'll contain numbers too, for example option no.6 is Brazil (Brazil id lets is 10). also titles(option labels) are dynamic too so they'll be repeated along side of their values.

    I'm going to confuse you here so the above description needs option name id or labels to set as text box value (maybe we could go with option labels)?! so forget name and id, could we just set text box value with chosen option label. the code in my first post equal chosen option value with text box value.

    Thanks
    Last edited by Datis; 09-08-2011 at 08:34 AM.

  • #4
    New Coder
    Join Date
    Mar 2011
    Posts
    41
    Thanks
    16
    Thanked 0 Times in 0 Posts
    resolved, thank guys.
    Last edited by Datis; 09-09-2011 at 08:33 AM.


  •  

    Posting Permissions

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