PDA

View Full Version : Copy last drop down item to text field



gwmbox
06-06-2017, 12:30 AM
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/


<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

gwmbox
06-11-2017, 02:21 AM
Solved

Missed adding


$(document).ready(function()

So, javascript is


$(document).ready(function() {
$('select').change(function(){

$('input[type=name]').val($('option:selected',this).text());
$('input[type=price]').val($(this).val());
});
});

Cheers