Results 1 to 2 of 2

Thread: Copy last drop down item to text field

  1. #1
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Question Copy last drop down item to text field

    Hi all

    I have a set of drop down fields where the child item is dependant on the parent, so for example

    Type
    --Box
    ----Red
    ------Item1
    ----Blue
    ------Item2
    --Bag
    ----Green
    ------Item3
    ----Blue
    ------Item4

    I also have a text field named Chosen Item

    What I want to do is when the last item is selected in the drop down box that the value is then copied to the text field. Obviously if the drop down item is changed now or later then that text field changes as well to the chosen final child drop down selected item

    I'm guessing some onevent javascript function is the best option but I really am struggling with where to start, all help appreciated, thanks

    If javascript is not the correct solution please help.

    EDIT - SAMPLE

    Here is something I have found on the net but when I try this it does not work for me, what is wrong here and how could I adapt it for the above?

    Copied from http://jsfiddle.net/f23uP/

    Code:
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <script type="text/javascript" src="//code.jquery.com/jquery-compat-git.js"></script>
    
    <title>Test copy select</title>
    <script type='text/javascript'>
    	$('select').change(function(){
    
    	$('input[type=name]').val($('option:selected',this).text());
    	$('input[type=price]').val($(this).val());
    	});
    </script>
    </head>
    
    <body>
    <form>
    	<select>
       	<option value="10">Apple</option>
       	<option value="20">Orange</option>
    	</select>
    
    	<input type="name" />
    	<input type="price" />
    </form>
    </body>
    </html>
    GW
    Last edited by gwmbox; 06-10-2017 at 02:15 AM. Reason: More info to try and get help
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  2. #2
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

    Solved

    Missed adding

    Code:
    $(document).ready(function()
    So, javascript is

    Code:
    $(document).ready(function() {
      $('select').change(function(){
    
        $('input[type=name]').val($('option:selected',this).text());
        $('input[type=price]').val($(this).val());
      });
    });
    Cheers
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

Similar Threads

  1. Replies: 2
    Last Post: 12-15-2010, 11:26 AM
  2. Populate a Text field via MYSQL Drop Down
    By xChAMaRx in forum JavaScript
    Replies: 0
    Last Post: 02-16-2009, 11:04 AM
  3. Replies: 3
    Last Post: 12-16-2008, 05:35 PM
  4. Replies: 0
    Last Post: 03-10-2007, 09:50 AM
  5. Form field copy killing me!
    By Ruberto in forum JavaScript
    Replies: 6
    Last Post: 01-31-2007, 06:49 PM

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
  •