I want to detect the when the ‘yes’ radio button is chosen with JavaScript. This is a small lightweight form and does not have jQuery or other library installed. My goal is for it to work in most major browsers and be written in just a few lines of code.
<script type="text/javascript">
if(document.forms[0].test.buy){alert("Clicked yes.);
</script>
</head>
<body>
<form name="test">
Buy Ice cream?
<input type="radio" name="buy" value="yes" /> Yes
<input type="radio" name="buy" value="no" /> No<br />
</form>
</body>
Your radio button needs to handle the onClick event –
then you’ll need a javascript function to handle the event