Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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?

irt.org | Knowledge Base | JavaScript | Form 8 | Q995 [ previous next ]

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?

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.