Topic: getElementById Radio checked

I'm trying to get a radio button to check using javascript but don't know how to assign the checked value. Default value is taken from a hidden form element.

function setdefault(id){

    var inputid="input" + id;
    var defaultvalueid="defaultvalue" + id;
   
    if(document.getElementById(defaultvalueid).value==0){
        document.setup.inputid[0].checked=true;
    }else{
        document.setup.inputid[1].checked=true;
    }
}

FF error console message
Error: document.setup.inputid has no properties


I have this working fine in xajax but it's a little slow so it looks like a job for javaScript

Re: getElementById Radio checked

Hi
You can check a radiobutton don`t using value (that is a different thing) you must change "checked" value to true
Something like this simple example(name, id, value and checked work in different moments wihtout connection ):

<script language="javascript">
function setdefault(id){
    defaultid=document.getElementById(id);
    defaultid.checked=true;
}
</script>
<body>
    <input type="radio" value="1" name="myradio" id="1" />
    <input type="radio" value="2" name="myradio" id="2" />
    <input type="radio" value="3" name="myradio" id="3" />
    <input type="radio" value="4" name="myradio" id="4" />
    <input type="button" value="check2" onclick="javascript:setdefault(2)" />
    <input type="button" value="check4" onclick="javascript:setdefault(4)" />
</body>

Last edited by Logus (2007-12-12 4:07:12 PM)

"Todo habito hace nuestra mano m

Re: getElementById Radio checked

Hi Logus,  I try not to leave old threads open but this one slipped by! I did end up using only javascript as xajax was too slow. Thanks for the reply. I'm sure it'll prove useful in the future, after I forget how to do it again!