/    Sign up×
Community /Pin to ProfileBookmark

uncheck individual radio buttons

hi, i have this code here to uncheck radio buttons individually but they don’t seem to work. i want to be able to uncheck individual radio buttons.

please help. thank you.

<script language=”JavaScript”>
<!–
function clearradio(formname,radioname) {
document.forms[formname].elements[radioname].checked = false;
}

//–>
</script>

<form name=Form1 action=”yourscript.asp” method=post>

<input type=radio name=option value=”Big” selected>Big
<input type=radio name=option value=”Small”>Small
<br>
<input type=radio name=number value=”1″ selected>1
<input type=radio name=number value=”2″>2
<br>
<input type=submit value=”Post Form”>
<input type=button value=”Clear Radio Buttons” onClick=”clearradio(‘Form1′,’option’);”>
<input type=button value=”Clear Radio Buttons” onClick=”clearradio(‘Form1′,’number’);”><br>

</form>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 26.2004 — Elements with the same name (radiobuttons your case) are in fact elements in an arrays. Thus:

document.forms[formname].elements[radioname]

is an array, not an individual element. To reffere array's elements you have to use the order of the elements in array

Your first button is

document.forms[formname].elements[radioname][color=red][0][/color]

The second is

document.forms[formname].elements[radioname][color=red][1][/color]

... and, an advice. 'option' and 'number' are reserved words in javascript so better change those names (with ,say, 'opt' and 'num') to avoid possible conflicts.
×

Success!

Help @jytioh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...