I am new to web programming and am having a problem calling a JavaScript function on a form button click. Instead of calling the function and executing the alert() it will reload the page, and reset the data that was entered into the text boxes. I am sure this is a simple error, but I am having trouble figuring this one out.
This is the code I have for my HTML form button
<input type="button" name="submitbutton" id="submitbutton" onclick="submit()" value="Submit Answer" />
And this is the function I am calling, without success
function submit(){
alert("working");
var answer=document.getElementById(answerText).value;
alert(answer);
if(movieArrayRand[0].answer==answer)
{
alert("You Win");
}
else{alert("You Lose");}
}
Any help would be great
If the javascript function is named something other than submit() the alert will be called (that is the re-named function will then be called when the button is clicked – submit is a function of the form object so there’s an ambiguity I believe and you’re seeing it being called rather than your javascript code)