Click to See Complete Forum and Search --> : Check radiobuttons
Hi all,
I've already searched the forum to find an answer to my question, but i didn't find it.
So i will give a very short example of what i'm trying to accomplish.
I have a page with radiobuttons, which are all not checked when the page loads. My question is.... Is it possible to make one of the buttons "checked" by using JavaScript?
Thanks in advance
Danny
AdamGundry
04-17-2003, 05:00 AM
Try this:
document.f1.choice.checked = true;
Adam
Thanks AdamGundry
I already did try that, but that doesn't seem to work...
I think there must be a solution though
If there are some more ideas, i'd be very happy to see them
Thanks
Greetzzz, Danny
cyberade
04-17-2003, 05:48 AM
<input type="radio" name="a" checked>
AdamGundry
04-17-2003, 05:53 AM
Ok, try setting the ids of the checkboxes, then using
document.getElementById('checkboxid').checked = true;
Adam
Hi again,
what do you mean by setting the id's?? isn't it enough to give them a name and value?? I haven't ever heard of setting the id's :confused:
Thanks for the response
Greetzz
Danny
Webskater
04-17-2003, 06:27 AM
<input type="radio" value="1" checked name="R1">
The code above puts a radio button on the page that is checked.
Setting the id's means giving the control an id as well as, or instead of, the name.
<input type="radio" value="V1" checked id="R1" name="R1">
If the control is on a form it will be referred to as:
formname.RI
If it is not on a form it can be referred to by its id:
RI
The id does not have to be the same as the name, it just helps for the sake of sanity.
Thanks for your reply Webskater,
I just figured out what id's are and how they work...
But your reply isn't quite what i'm looking for... I already know how to check the radiobuttons :)
I want all of the buttons to be NOT checked when the page opens, and i.e. if someone clicks a button "Yes" , the radiobutton "Yes" should get checked... Else the other radiobutton should get checked...
Hope this is clear
Greetzzz
Danny
Webskater
04-17-2003, 06:36 AM
Sorry, had not read your text file.
Try:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function checkbutton()
{
for (i=0;i<f1.choice.length;i++)
{
if (f1.choice.value == 'Yes') fi.choice.checked= 'true'
}
}
</SCRIPT>
</HEAD>
<BODY onload=checkbutton()>
<FORM name="f1">
<input type="radio" name="choice" value="Yes"> <font size="2">Yes</font></input>
<input type="radio" name="choice" value="No"> <font size="2">No</font></input><br/>
</FORM>
</BODY>
</HTML>
Back again,
I tried your code, but it doesn't do a thing :confused: ...
To put it in a simple way, i just need the JavaScript code to check or uncheck radiobuttons...
I thought this was not that difficult to manage, but i have thought wrong i believe :eek:
Thanks for any reply :D
Greetzzz
Danny
cyberade
04-17-2003, 07:28 AM
:confused: Radio buttons already get checked when you click them
and if there is more than one radio button with the same name
all of the others become unchecked.
Webskaters code ( if you change his 'if' statement to "if (f1.choice.value == 'Yes') f1.choice[i].checked= 'true'") will cause the Yes option to be checked when the page is loaded.
Do you want it so that both buttons could be checked?
Just make the names/ids different.
OR
Are you saying that you want [I]another button on the page which,
when pressed, will cause one of the Yes/No options to become
checked?
Then add to your form:
<input type="button" name="b1" value="Yes" onClick="checkButton('Yes');">
<input type="button" name="b2" value="No" onClick="checkButton('No');">
(remove the onLoad part from the BODY tag) and use the function:
function checkButton(butt){
if (butt == "Yes"){
document.f1.choice[0].checked=true;
document.f1.choice[1].checked=false;
}
else{
document.f1.choice[1].checked=true;
document.f1.choice[0].checked=false;
}
}
Thanks everybody, i've found a solution
It was a bit of a mixture of all the solutions you guys suggested
Thanks again
Greetzzz
Danny