I have a dropdown which when an option is selected it needs to populate an input value with the value of the option selected only its not working as in nothings changing, Could anybody see and explain why going of my code?
<select id="corr_sel" onchange="updatecredoff();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<div id="cr_off"><?php echo $user_curr_coff;?></div>
function updatecredoff() {
if(value == 1) {
document.getElementById("corr_sel").value = '1';
document.getElementById("cr_off").innerHTML = '1';
}
else if(value == 2) {
document.getElementById("corr_sel").value = '2';
document.getElementById("cr_off").innerHTML = '2';
}
else if(value == 3) {
document.getElementById("corr_sel").value = '3';
document.getElementById("cr_off").innerHTML = '3';
}
else if(value == 4) {
document.getElementById("corr_sel").value = '4';
document.getElementById("cr_off").innerHTML = '4';
}
else if(value == 5) {
document.getElementById("corr_sel").value = '5';
document.getElementById("cr_off").innerHTML = '5';
}
else if(value == 6) {
document.getElementById("corr_sel").value = '6';
document.getElementById("cr_off").innerHTML = '6';
}
else if(value == 7) {
document.getElementById("corr_sel").value = '7';
document.getElementById("cr_off").innerHTML = '7';
}
else if(value == 8) {
document.getElementById("corr_sel").value = '8';
document.getElementById("cr_off").innerHTML = '8';
}
else if(value == 9) {
document.getElementById("corr_sel").value = '9';
document.getElementById("cr_off").innerHTML = '9';
}
else {
document.getElementById("corr_sel").value = '10';
document.getElementById("cr_off").innerHTML = '10';
}
};
You haven’t defined
valueanywhere in your code (at least from what you’ve shown) before this:You could define
onchange="updatecredoff(this);withthisattached, and then in your function before your first conditional assign the value:Example:
http://jsfiddle.net/niklasvh/yXHD5/
Or you could just get the value by the ID like this:
If that is all the functionality you intend to do with the function, you could simplify it to just: