Issue
I'm still learning Javascript and I need to get input type text value to javascript code and convert it to integer. but everytime it display "Your fail" even i input higher mark. I need help to find my error.
function mark() {
var sMark = document.form.mark.value;
var myMark = parseInt(sMark);
if (myMark > 75) {
document.write("You pass exam with Higher mark");
} else if (myMark > 50) {
document.write("You pass exam");
} else {
document.write("You Fail");
}
}
<form action="" name="form">
Enter Your Mark: <input type="text" name="mark" id="mark1">
<input type="submit" value="Submit" onclick="mark()"><br>
</form>
Solution
The input with the name "mark"
is shadowing the global mark
function in the inline onclick
handler. Either rename those or directly call window.mark()
.
function mark() {
var sMark = document.form.mark.value;
var myMark = parseInt(sMark);
if (myMark > 75) {
document.write("You pass exam with Higher mark");
} else if (myMark > 50) {
document.write("You pass exam");
} else {
document.write("You Fail");
}
}
<form action="" name="form">
Enter Your Mark: <input type="text" name="mark" id="mark1">
<input type="submit" value="Submit" onclick="window.mark()"><br>
</form>
Answered By - Unmitigated Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.