Forum Moderators: open
function someFunction(somevar) {
//somevar is the value of the textbox calling the function on an onBlur
if(somecondition) {
alert("Please make sure you enter VALID information");
this.value="";
this.focus();
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
window.onload=function() {
if (document.getElementById('mybox')) {
document.getElementById('mybox').onblur=function() { someFunction(this); };
}
};
function someFunction(somevar) {
// somevar is now a reference to the OBJECT
// verify this by uncommenting these alerts
// alert(somevar.id);
// alert(somevar.value);
if(somevar.value=='') {
alert("Please make sure you enter VALID information.");
somevar.value='Please enter valid information.';
somevar.focus();
}
}
</script>
</head>
<body>
<form>
<textarea name="mybox" id="mybox">Please enter valid information.</textarea>
<input type="button" value="This button won't do anything">
</form>
</body>
</html>
"this" in that context refers to the function itself.
<textarea name="mybox" id="mybox" onblur="someFunction()">
document.getElementById('mybox').onblur=function() { someFunction(); }; document.getElementById('mybox').onblur = someFunction; document.getElementById('mybox').onblur=function() { someFunction(this); }; Doesn't the context (and the value of the 'this' keyword) inside the event handler (ie. function) depend on how the event handler is registered?