the problem I have with the following script was that no matter what response the result still came out as "Nope!"... any ideas?
function ques1() {
var x=document.forms['ques1'].elements['response1'];
if (x==2) {
document.forms['ques1'].elements['result1'].value="Correct!";
}
else {
document.forms['ques1'].elements['result1'].value="Nope!";
}
}
function ques1() {
var x=document.forms['ques1'].elements['response1'];
if (x="2") {
document.forms['ques1'].elements['result1'].value="Correct!";
}
else if (x!="2"){
document.forms['ques1'].elements['result1'].value="Nope!";
}
}
and instead it says "correct!" no matter what i put in
Your problem is that you try to use a submit button, and the submit action to do something in JavaScript, which is an error. The submit process changes the session, and any changing of the session will make all the javascript calculation lost.
You may block the submit action by returning false:
Agree. But in his particular case that is not enough (see my comment above)
actually, weird as it is, in this case it is enough (works in FF, Chrome & IE at least)... look at the form action... it seems the antiquated "javascript:" blocks the submission action
but I would agree - there's no point in a submit button that is not actually submitting anything.
on a side note, the function could easily be simplified using the ternary...
Code:
function ques1() {
var x=document.forms['ques1'].elements['response'].value;
document.forms['ques1'].elements['result'].value=x=="2"?"Correct!":"Nope!";
}
Bookmarks