Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q995 How can I allow the user to select one from a number of radio button options, then click on a button which will take them to a web page as indicated by their selection?

You are here: irt.org | FAQ | JavaScript | Form | 8 | Q995 [ previous next ]

Try:

<html>
<head>

<script language="JavaScript"><!--
function go(what) {
    for (var i=0; i<3; i++) {
        if (what.sameName[i].checked == true)
            location.href = what.sameName[i].value;
    }
}
//--></script>

</head>

<body>

<form>
<br><input type="radio" name="sameName" value="http://www.irt.org/script/faq.htm"> JavaScript FAQ
<br><input type="radio" name="sameName" value="http://www.irt.org/articles/script.htm"> JavaScript Articles
<br><input type="radio" name="sameName" value="http://www.irt.org/games.htm"> JavaScript Games

<p>

<input type="button" onClick="go(this.form)" value="Go">
</form>

</body>

If you need a default location, in case the user does not select one of the radio buttons, then use the following go() function instead:

function go(what) {
    var chosen = false;
    for (var i=0; i<3; i++) {
        if (what.sameName[i].checked == true) {
            chosen = true;
            location.href = what.sameName[i].value;
        }
    }
    if (!chosen)
        location.href = 'defaultLocation.htm';
}

Feedback on 'Q995 How can I allow the user to select one from a number of radio button options, then click on a button which will take them to a web page as indicated by their selection?'

©2018 Martin Webb