this is a classic question but can’t find the best approach. I have a dropdown with id project_billing_code_id and 3 values (1, 2, 3).
If value selected = 1 then show div with id one and only this one. div two and three must be hidden.
I also want to make this happen when view is loaded so not only on change.
$(document).ready(function() {
$("#hourly").hide();
$("#per_diem").hide();
$("#fixed").hide();
$("#project_billing_code_id").change(function() {
if ($("#project_billing_code_id").val() == '1') {
$("#hourly").show();
}
else if ($("#project_billing_code_id").val() == '2') {
$("#per_diem").show();
}
else if ($("#project_billing_code_id").val() == '3') {
$("#fixed").show();
}
else {
$("#hourly").hide();
$("#per_diem").hide();
$("#fixed").hide();
}
});
});
You were close. You probably want a few small tweaks to the behaviours to make sure all the divs hide and that correct div is showing on page load.
Have a play with the code here: http://jsfiddle.net/irama/ZFzrA/2/
Or grab the updated JS code here:
Let us know how you go!