I dont know why the radiobutton wont set checked. I tried all the methods i could find but it won't work.
How I want it to work is that Type1 radiobutton is set checked if the value is 1; if the value is 0, Type2 radiobutton is set checked.
I printed the value in a textfield. The value is from a database btw. Nothing's wrong with getting the value from the database and printing it in the textfield.
My only problem is setting the radio buttons checked based on the value in the textfield.
I put an alert() in the function but it seems that it wont display an alert too. I think it doesn't call the function from the commmon.js. But other functions in common.js works though. :/
Here's the code;
common.js
var selected;
function SetRadiobuttonValue()
{
selected = document.getElementById('myType').value;
alert(selected);
if(selected == "" || selected == 0){
document.getElementById("type1").checked = true;
document.getElementById("type2").checked = false;
}else if(selected == 1){
document.getElementById("type1").checked = false;
document.getElementById("type2").checked = true;
}
}
form.jsp
<input type="text" id="myType" name="admin" value="${usersModule.adminFlag}" onChange=" SetRadiobuttonValue()">
<label><input type="radio" name="type" id="type1" value="0" > General User</label>
<label><input type="radio" name="type" id="type2" value="1" > Admin</label>
To those who tried to answer but still wont work:
I have another example: Imagine editing a profile form and there is the radio buttons of Male and Female. If it is Male in the profile info, the Male radio button is set checked or vice versa.
That's what i want to happen. But the radio button wont set checked. :(
UPDATE
I found the answer now. Thanks to @ketan :D I just have to put the function on body tag onLoad event to make it work. :D