Click here to Skip to main content
15,991,287 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. The problem is if i select a button and click submit it is redirecting me to a certain page but on page load it needs to check a radio button and if i click submit it gives an alert but if it selected also it needs to redirect to certain page.
XML
<SCRIPT LANGUAGE="JavaScript">
function go(loc) {
    if(loc ==''){
        alert("please select a radio button");
    }
    else
        window.location.href = loc;
}
function goTo(loc1){
    document.form.selectedLoc.value = loc1;
}
</script>
</HEAD>

<BODY>
<form name="form">
<input type="radio" name="loc" checked="checked" onClick="goTo('google.html');" > google<br>
<input type="radio" name="loc" onClick="goTo('yahoo.html');"> yahoo<br>
<input type="radio" name="loc" onClick="goTo('facebook.html');"> facebook<br>
<input type="button" name="submit" value="submit" onclick="go(document.form.selectedLoc.value)"/>
<input type="hidden" name="selectedLoc" value=""></input>
</form>

thanks for your help.
Posted
Updated 1-Aug-11 9:03am
v2

1 solution

You can call the javascript function goTo('google.html'); using onload event inside body tag.
HTML
<body onload="goTo('google.html');"></body>


Or another solution using jQuery.
HERE is the ref.
 
Share this answer
 
Comments
Member 8046452 1-Aug-11 15:34pm    
giving the same alert when i use onload also.
Member 8046452 1-Aug-11 18:09pm    
got it using the onload
Monjurul Habib 2-Aug-11 3:24am    
great

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