View Full Version : I need help adding parameters to attachEvent and addListenerEvent

04-06-2005, 04:21 PM
My very first post to these forums! :thumbsup:

Anyway, all of the examples that I have seen both on the web and in various javascript tutorial sites about addEventListener or attachEvent use a function that has no parameters, e.g.:

<script type="text/javascript">
function alertFunc() {
alert('Hello World');

//For Mozilla/Netscape
//For internet Explorer

I have tested and tried that, and it works just fine for me. However, what I really want to do, is to add an event listener with a function that has parameters, such as the below code is attempting to do:

<script type="text/javascript">
years = new Array( 'tier1_1996','tier2_1996','tier3_1996','tier4_1996','tier5_1996' );
if( document.getElementById("someElementId").addEventListener ) {
document.getElementById("someElementId").addEventListener('onblur',function() { addValues(years,'containerElement') },false);
} else if ( document.getElementById("someElementId").attachEvent ) {
document.getElementById("someElementId").attachEvent('onblur',function() { addValues(years,'containerElement') });

In the element specified by "someElementId" there is already an onblur event, and in Internet Explorer, it takes the new event and gets rid of the old one completely, while in mozilla, it keeps the old event but doesn't add the new event that I specified. If anyone has struggled through this situation and has the solution, I would reallllllly appreciate it.

If not, I will be forced to declare smiley war!

04-06-2005, 05:11 PM
Yeah it's perfectly doable, and your syntax looks basically right, but "addEventListener" only takes the name of the event "blur" rather than "onblur"

Have a look at my encapsulated anonymous handlers (http://www.codingforums.com/showthread.php?t=47379) thread - that expands on the basic technique, so you might find that useful.

04-06-2005, 06:29 PM
Brothercake, thanks for the excellent post, and the excellent encapsulated post as well... I sure wish I would have found that when I first searched the forums!

I know that this is a javascript faux paus (according to all of the theory that I have been reading on the web) but I have an onblur event inside of a regular input element that I have called the old-fashioned way:

<input id="someId" name = "someName" onBlur="someFunction()">

I know that this the wrong way to do events, and I am going to change it, but my question is- how does the addListenerEvent/attachEvent concept work with events that have been added in the old style way? I would assume that it leaves all of the other events alone and just puts in another event, but if there are possibly weird interactions, this would seem a place where they would happen...

04-06-2005, 06:33 PM
It should do exactly what you're expecting - add new listeners without affecting any attribute event handlers or expandos (object.onevent) which already exist.

They're better because they provide this encapsulation - so if you're writing a script for other people to use, you can be confident it won't affect any other scripting they're already doing. And of course they also mean you can have multiple listeners yourself on the same object.

Attribute event handlers are often frowned upon - and rightly so imo - partly because they're non semantic, but mostly because they're tag soup - they mix content and logic - it's better to keep the logic in an entirely separate layer, hence DOM scripting.

So essentially, your approach is entirely sound for exactly the reasons you've described. What kind of weird interactions are you getting?

04-06-2005, 08:43 PM
As with most things... It turns out that the weird interaction was due to an error in my code. Too many times I blame internet explorer/ mozilla for my own poor coding... ;) Anyway thanks for the description. I have been developing web pages with php for about 3+ years, and I am only now starting to see the joys of client side scripting. While it can be a little more frustrating than server side sometimes (because the standards compliance, etc.) it is INCREDIBLE what you can do with javascript. :thumbsup:

04-07-2005, 08:55 PM
While it can be a little more frustrating than server side sometimes (because the standards compliance, etc.) it is INCREDIBLE what you can do with javascript. :thumbsup:
Well exactly .. it is very annoying having to deal with multiple intepretations of the same thing all the time, but it's worth it - DOM scripting allows for the kind of real-time interactivity that would otherwise not be possible.