How To Display The Corresponding Values In The Text Box Depending On Drop Down List On Selecting Radio Button In Javascript?
I am creating a form which ask to select degree using radio buttons. Depending on radio button selected, values in the drop down list changes. Now I want to display some value in t
Solution 1:
document.getElementById ("textboxId").value = "value";
Solution 2:
You should consider putting your Options
in an object instead of using a chain of if statements, not only is it easier to look at, but it's also easier to extend later on and you are caching the values.
(function() {
var options = {
UG: [
newOption('Select One', '0'),
newOption('B.Tech', '1'),
newOption('B.E', '2')],
PG: [
newOption('Select One', '0'),
newOption('M.Tech', '3'),
newOption('M.C.A', '4')],
fallback: [
newOption('Select Degree First', '00')]
}
varSetBranchBydegree = function(degree) {
var dropdown = document.getElementById("degreepg");
if (options[degree] !== undefined) {
dropdown.options = options[degree];
} else {
dropdown.options = options.fallback;
}
}
var init = function() {//Setup listeners
$("#gereepg").change(function(event) {
var textBox = $("#textbox"), //The textbox with id textbox
index = event.target.index;//selected indexif (index == 1 || index == 2) {
textbox.val("8");
}
elseif (index == 3) {
textbox.val("4");
}
elseif (index == 4) {
textbox.val("6");
}
});
}
$(document).ready(init);//run the init method when the site has loaded.
})();
For changing the value of the textbox you need to listen to the onChange event of the select. This can be done by simply adding an attribute onChange="javascriptMethod(this)", but this is generally not recommended because it promotes global scope pollution. Above is an example of doing it with jQuery;
Post a Comment for "How To Display The Corresponding Values In The Text Box Depending On Drop Down List On Selecting Radio Button In Javascript?"