473,468 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Event handler scope weirdness

Hi,

I'm writing a function to validate fields, and I'm confused about
variable scope. Here's what I have:

function RequiredField(name, error_message, error_display_id) {
this.name = name;
this.error_message = error_message || 'Required field';
this.error_display_id = error_display_id || name + '-message';

this.onblurrer = function () {

//this is the strange part

alert('this.check: ' + this.check + ' this.showErrorMessage: ' +
this.showErrorMessage);
this.check();
this.showErrorMessage();
return true;
};

$(this.name).onblur = this.onblurrer
}
RequiredField.prototype.check = function () {
alert('check: this.name: ' + this.name);
return !!$(this.name).value;
}
RequiredField.prototype.showErrorMessage = function () {
alert('show');
$(this.error_display_id).innerHTML = this.error_message;
}

Going into a javascript shell, I find that if I call an RequiredField
object's onblurrer directly, it does what I expected, but if I call
$(name).onblur() (with $ being prototype's shortcut for
getElementById), the alert box shows that this.check and
this.showErrorMessage are undefined, which is also what happens after
the real onblur event in the browser.

I'm pretty much stumped by this. I thought Javascript was lexically
scoped, which would mean that onblurrer shouldn't change just because
of who calls it. Are there exceptions to lexical scope?

Nov 23 '05 #1
1 1435
OK, I figured this out. 'this' is not a variable and it's provided by
the caller (IOW it's dynamically scoped) so the answer is to do
something like

function Foo() {
var that = this
....
onclick = function () {that.bar}
}

Too bad the word 'this' is effectively ungoogleable.

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
2
by: Ken Varn | last post by:
If an object of class A adds an event handler to and object of class B, and class A is finalized, does the handler delegate get removed from class B's event handler or is there a bad pointer on...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
9
by: mike | last post by:
I have this script executing <script> function mike_test(event) { x = window.event.clientX; alert(x); } </script> <iframe src="blank.html" id="my_iframe1"> </iframe>
15
by: prabhdeep | last post by:
Hi, Can somebody explain, why in following code, i get "event not defined" error funcTest(sMenu) { doument.getElementById('id1').addEventListener('mousedown', function(){ click(sMenu,...
17
by: dan_williams | last post by:
I have the following test web page:- <html> <head><title>Test</title> <script language="Javascript"> <!-- function fnTR() { alert("TR"); }
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
6
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
3
by: Tuxedo | last post by:
Is it possible to add a function call to the onUnload event handler from an external js file, or can this only be done via the body tag? In any case, I presume there can only exist one onUnload...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.