This code below for some reason doesn't work when trying to redirect. I have tried several other ways such as window.location.href and more. The only thing that works is window.open ("")
but this opens a page in a new window when I need it to be on the same. If I do window.open("", "_self")
then it does not work again. If I replace the window.location
with an alert
it works fine, so I think the overall code is normal just something preventing it from redirecting on the same page. This issue is also on my Windows Chrome and a Mac Firefox.
<html>
<head>
<script type="text/javascript">
function checkAnswers(){
getElementById("myQuiz");
if(myQuiz.elements[1].checked){
window.location = "www.google.com";
}else{
alert("Failed");
}
};
</script>
</head>
<body>
<img src="Castle.JPG" />
<form id="myQuiz" onsubmit="checkAnswers()" action="">
<p>Name the country this castle is located in?
<br/>
<input type="radio" name="radiobutton" value="A"/>
<label>England</label>
<input type="radio" name="radiobutton" value="B"/>
<label>Ireland</label>
<input type="radio" name="radiobutton" value="C"/>
<label>Spain</label>
<input type="radio" name="radiobutton" value="D"/>
<label>France</label>
<input type="submit" name="submit" value="Submit"/>
<input type="reset" name="reset" value="Reset"/>
</p>
</form>
</body>
</html>
getElementById( "myQuiz" );
should bevar myQuiz = document.getElementById("myQuiz");
– Frolick<input />
. It is incorrect. – Askins(Invalid)
– Askins<title></title>
in the<head></head>
section and the error goes away. This has nothing to do with self-closing tags. – Ardelia<div />
will be interpreted as<div>
, not<div></div>
as in XHTML. The code looks (arguably) nicer and (arguably) more semantic, but it has no benefit beyond that. – Jacquesjacquet