...

View Full Version : Change input value based on select value?



speck
07-19-2011, 07:06 PM
Hello, I am trying to change the value of #name and #category inputs based on the value of select. A friend gave me the code below, but I can't get it to work. Thanks so much for your help



<script type="text/javascript">
$('#Gift_Type').change(function(){
if ($(this).val() == 'Monthly'){
$('#name').val('Partnership')
$('#category').val('Partnership')
} else {
$('#name').val('Donation')
$('#category').val('Donations')
}
})
</script>




<SELECT NAME="sub_frequency" ID="Gift_Type" onchange="OnSelectionChanged (this)">
<OPTION VALUE="1m">Monthly</OPTION>
<OPTION VALUE="">One Time</OPTION>
</SELECT>




<input type="text" id="name" name="name" value="Partnership" />
<input type="text" id="category" name="category" value="Partnership" />

devnull69
07-19-2011, 07:17 PM
1. Please remove onchange="OnSelectionChanged (this)" from the SELECT
2. Please wrap your remaining jQuery code in


$(document).ready(function() {
// your code here
});

to make sure that the element exists before you try to attach an event handler to it

3. The value of the first option is "1m" and not "Monthly"

speck
07-19-2011, 07:32 PM
Thank you so much for your help, but I made suggested changes, but it still doesnt work. I have included the rest of my javascript (may be what is tripping it up) I need to keep the "OnSelectedChange" Thank you so much for your help!



<script type="text/javascript">
$(document).ready(function(){
$('#1y').hide();
$('#One_Time').hide();
$("#Gift_Type").change(function(){
$("#" + this.value).show().siblings().hide();
});
$("#Gift_Type").change();
});
</script>

<script type="text/javascript">
$('#Gift_Type').change(function(){
var $sel = $(this);
if($sel.val() == ''){
$sel.attr('name','');
} else {
$sel.attr('name','sub_frequency');
}
});
</script>

<script type="text/javascript">
$(document).ready(function() {
$('#Gift_Type').change(function(){
if ($(this).val() == '1m'){
$('#name').val('Partnership')
$('#category').val('Partnership')
} else {
$('#name').val('Donation')
$('#category').val('Donations')
}
})
});
</script>




<SELECT NAME="sub_frequency" ID="Gift_Type" onchange="OnSelectionChanged (this)">
<OPTION VALUE="1m">Monthly</OPTION>
<OPTION VALUE="">One Time</OPTION>
</SELECT>




<input type="text" id="name" name="name" value="Partnership" />
<input type="text" id="category" name="category" value="Partnership" />

devnull69
07-19-2011, 07:38 PM
Sorry, but what do you need OnSelectionChanged() for? It is nowhere in your code

And you still have parts of the code not wrapped in $(document).ready(). You should definitely start to debug your code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum