[thelist] [JavaScript] Event question for you event experts

Tom Dell'Aringa pixelmech at yahoo.com
Mon Feb 20 11:12:43 CST 2006


Events have always been my weak point, I must admit. Anyway, in this current project, we're doing
various things when the page loads, using the onload event, as you would expect. I did the typical
thing one might do in this case - built an init function and did like so:

function initPage()
{
  addFocusControls();
  isErrors();
  buttonState();
}

window.onload = initPage;

It's one way to do things that works, I suppose. In any event (no pun intended) I think this lacks
some flexibility. For example, there is an issue where I have a background image on a text field.
If the user goes backwards, the image can end up being in the field behind some text, which is
bad.

Essentially, I want that text field to check itself on page load, and see if it has text in it,
and if it does, I want to clear that background image so it doesn't clutter up the actual text.

It seems like the way to go would be to add an event listener to that text field, listening for
the load event. Then check for a value, and remove image as needed. But it seems that using an
event listener for the onload event is problematic. Say I want to do this:

var myInput = document.getElementById('myTextInput');
myInput.addEventListener("load", function(e){ //stuff here }, false);

But the problem is the event listener can only be applied to myInput AFTER the onLoad event...
sooo... how can it listen for onload!?!? I seem to be caught in a circle here.

This application has a lot of event driven interaction, so I am trying to move to a OO way of
handling events instead of attaching them everywhere willy nilly. But this issue has me
stuck...any suggestions are greatly appreciated!

Tom



http://www.pixelmech.com/
http://www.crossandthrone.com/


Professor Rumford: 'But I still don't understand about hyperspace.' 
The Doctor: 'Well, who does?' 
K9: 'I do.' 
Doctor: 'Shut up, K9!' 





More information about the thelist mailing list