for the following form
<form action ='search.php' method = 'post'>
<select name="filter">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>
<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />
<input type ='submit' value = 'Search' />
</form>
How do i make the value of the input field change based on whether A
or B
is selected? I assume it has to be done in javascript. I have used jquery here and there so it is ok to make suggestions based on it. Thanks alot!
for the following form
<form action ='search.php' method = 'post'>
<select name="filter">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>
<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />
<input type ='submit' value = 'Search' />
</form>
How do i make the value of the input field change based on whether A
or B
is selected? I assume it has to be done in javascript. I have used jquery here and there so it is ok to make suggestions based on it. Thanks alot!
4 Answers
Reset to default 9You don't have to use jQuery, (but jQuery does makes life easier):
HTML:
<form action ='search.php' method = 'post'>
<select id="filter" name="filter">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>
<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />
<input type ='submit' value = 'Search' />
</form>
Javascript:
document.getElementById('filter').onchange = function () {
document.getElementById('field').value = event.target.value
}
Example
you may try this
<script type="text/javascript">
function changeValue(){
var option=document.getElementById('filter').value;
if(option=="A"){
document.getElementById('field').value="A Selected";
}
else if(option=="B"){
document.getElementById('field').value="B Selected";
}
}
</script>
<form action ='search.php' method = 'post'>
<select name="filter" id="filter" onchange="changeValue();">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>
<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />
<input type ='submit' value = 'Search' />
</form>
give id filter
to select
$(document).ready(function(){
var text_val;
$('#filter').change(function(){
text_val = $(this).val();
$('#field').attr('value',text_val);
return false;
});
});
Try....
$('select[name=filter]').change(function() {
var input_field = $('input#field');
switch ($(this).val()) {
case 'A': input_field.val('Value if A is chosen'); break;
case 'B': input_field.val('Value if B is chosen'); break;
}
});