Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  20] [ 2]  / answers: 1 / hits: 153956  / 15 Years ago, sat, may 2, 2009, 12:00:00

New to javascript, but I'm sure this is easy. Unfortunately, most of the google results haven't been helpful.



Anyway, I want to set the value of a hidden form element through javascript when a drop down selection changes.



I can use jQuery, if it makes it simpler to get or set the values.


More From » jquery

 Answers
9

If you have HTML like this, for example:



<select id='myselect'>
<option value='1'>A</option>
<option value='2'>B</option>
<option value='3'>C</option>
<option value='4'>D</option>
</select>
<input type='hidden' id='myhidden' value=''>


All you have to do is bind a function to the change event of the select, and do what you need there:



<script type='text/javascript'>
$(function() {
$('#myselect').change(function() {
// if changed to, for example, the last option, then
// $(this).find('option:selected').text() == D
// $(this).val() == 4
// get whatever value you want into a variable
var x = $(this).val();
// and update the hidden input's value
$('#myhidden').val(x);
});
});
</script>


All things considered, if you're going to be doing a lot of jQuery programming, always have the documentation open. It is very easy to find what you need there if you give it a chance.


[#99608] Tuesday, April 28, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaylynbrynnk

Total Points: 706
Total Questions: 98
Total Answers: 91

Location: Israel
Member since Thu, Jan 7, 2021
3 Years ago
;