Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


i have the following code with radio buttons it is giving an alert with radio button value after the selection but is it possible to link a website like google.com to the selection.

XML
<script language="javascript">
<!--
function RadioCheck() {

var selection = document.quiz.radio;
for (i=0; i<selection.length; i++)
  if (selection[i].checked == true)
  alert(selection[i].value)
}
//-->
</script>

XML
<form name="quiz">
  <input type="radio" name="radio" value="red">red<br>
  <input type="radio" name="radio" value="orange">orange<br>
  <input type="radio" name="radio" value="yellow">yellow<br>

XML
<input type="submit" value="Check Answer" onClick="RadioCheck()">
</form>


Thanks in advance for your help.
Posted

Use Window.Location or Window.Open
JavaScript
<script language="javascript">
<!--
function RadioCheck() {
 
var selection = document.quiz.radio;
for (i=0; i<selection.length; i++)
  if (selection[i].checked == true)
  window.location = "http://www.google.com/";
}
//-->
</script>
 
Share this answer
 
Comments
Member 8046452 27-Jul-11 18:56pm    
Thanks dude but it is not working.
thatraja 27-Jul-11 18:58pm    
Any error message are you getting? check in javascript degugger
Member 8046452 27-Jul-11 20:07pm    
No, it is not showing any error. It is reloading the same page again.
Because you're using a submit button, you need to return false to prevent the form submitting:

XML
<script language="javascript">
<!--
function RadioCheck() {
var selection = document.quiz.radio, urls = [
  'http://www.google.com/', 'http://www.bing.com/', 'http://yahoo.com/'
];
for (i=0; i<selection.length; i++)
  if (selection[i].checked)
    window.location = urls[i];
  return false; // ADD THIS!
}
//-->
</script>


You need a return in the onclick too:
<input type="submit" value="Check Answer" onClick="return RadioCheck()">
 
Share this answer
 
Comments
Member 8046452 28-Jul-11 13:46pm    
It is not going from there but i change it like this it is working but i need it on submit button

<SCRIPT LANGUAGE="JavaScript">

function go(loc) {
window.location.href = loc;
}

</script>

</HEAD>



<BODY>

<form name="form">
<input type="radio" name="loc" önClick="go('http://www.google.com/');"> google<br>
<input type="radio" name="loc" önClick="go('http://www.yahoo.com/');"> yahoo<br>
<input type="radio" name="loc" önClick="go('http://www.facebook.com/');"> facebook<br>
</form>

</BODY>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900