Reputation: 127
What I want to do is to be able to change the value of a label when I make a change in a dropdown select form.
<select form="FormCalcInput" id="calcOption" title="Choose what value you want to count>
<option value="PMT" class="PMT">Payment (PMT)</option>
<option value="I" class="I">Interest (I)</option>
<option value="FV" class="FV">Future value (FV)</option>
<option value="PV" class="PV">Present value (PV)</option>
</select>
<label id="labelChanged"></label>
So the label should be the previous selected value.
Thanks.
Upvotes: 1
Views: 9275
Reputation: 33870
If you want the previous selected, you need a global variable :
var newLabel = '';
$('#calcOption').on('change', function(){
$('#labelChanged').text(newLabel); //Change the text before changing the value
switch(this.value){
case 'I':
newLabel = 'Interest';
break;
case 'FV':
newLabel = 'Future value';
break;
case 'PV':
newLabel = 'Present value';
break;
case 'PMT':
newLabel = 'Payment';
break;
}
}).trigger('change');
Upvotes: 1
Reputation: 79
you can call this kind of code on change event of dropDown
Var ddlSelectedValue = $('#calcOption').text();
$('#labelChanged').text(ddlSelectedValue );
Upvotes: 0
Reputation: 235
Please try this:
function TestFunction() {
var name = $("#calcOption option:selected").text();
if (name == "Interest (I)") {
$("#labelChanged").val("");
$("#labelChanged").text("Payment");
}
else if (name == "Interest (I)") {
//set other value
}
}
Upvotes: 0
Reputation: 318182
I'm guessing it should be like this.
$('#calcOption').on('change', function() {
var prev = $(this).data('prev') || $('option:first', this).text();
$('#labelChanged').text(prev);
$(this).data('prev', $('option:selected', this).text());
});
This saves the previously selected value and sets it as the labels text
Upvotes: 1
Reputation: 2815
$(document).ready(function(){
var previous = $('#calcOption').val();
$('#calcOption').change(function(){
var val = $(this).val();
$('#labelChanged').html(previous);
previous = val;
});
});
Updated
Upvotes: 1
Reputation: 1796
try this
<select form="FormCalcInput" id="calcOption" title="Choose what value you want to count" onChange="changelabel(this.value)">
<option value="PMT" class="PMT">Payment (PMT)</option>
<option value="I" class="I">Interest (I)</option>
<option value="FV" class="FV">Future value (FV)</option>
<option value="PV" class="PV">Present value (PV)</option>
</select>
<label id="labelChanged"></label>
in javascript
var labelname;
function changelabel(var value){
switch(value){
case 'I':
case 'FV':
labelname = 'Payments';
break;
case 'PMT':
}
$('#labelChanged').html(labelname);
}
Upvotes: 0
Reputation: 16438
I am not sure if this is what you are looking for, but this sets the label to the text from the option selected
$('#calcOption').on('change', function(){
var v = $(this).find(':selected').text();
$('#labelChanged').text( v );
});
Upvotes: 0