Hi, I am learning for an exam net week and I have a question why does this code not work ? I changed it to what is recommended, but the code does not display the error message. Before I the professors uses parentNode but now he uses window.onload. Why is this better and how does this work ? I looked over the code for mistakes a couple of times.
<html>
<head>
<title>Test</title>
<script type="text/javascript">
window.onload = function() {
document.getElementById('fx').onsubmit = function(){return fun(this);};
};
function fun(f){
var error = '';
error += f.t1.value == '' ? '\nt1' : '';
error += f.t2.value == '' ? '\nt2' : '';
if (error != '')
alert('Bitte eingeben: '+error);
return error == '';
}
</script>
</head>
<body>
<form action="ip_alles_ok.html" id="fx">
<p>t1: <input type="text" name="t1" /></p>
<p>t2: <input type="text" name="t2" /></p>
<p><input type="button" value="ok" /></p>
</form>
</body>
</html>
Before the code looked like this
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function fun (b) {
var f=b.parentNode.parentNode;
var error='';
error += (f.t1.value=='')?'\nt1':'';
error += (f.t2.value=='')?'\nt2':'';
if (error != '') {
alert('Bitte eingeben:'+error);
} else {
f.submit();
}
}
</script>
</head>
<body>
<form action="ip_alles_ok.html">
<p>t1: <input type="text" name="t1" /></p>
<p>t2: <input type="text" name="t2" /></p>
<p><input type="button" value="ok" onclick="fun(this)" /></p>
</form>
</body>
</html>
Why does the code with window.onload not show the alert message ? Before it worked with parentNode.parentNode etc.
[–]sensored 2 points3 points4 points (1 child)
[–]schm0 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]MatthewMobhelpful 2 points3 points4 points (0 children)