Change the value of a label when the selected form is changed with jQuery

advertisements

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.


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');