Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Texas, USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question I need help adding parameters to attachEvent and addListenerEvent

    My very first post to these forums!

    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
    document.getElementById('someElementId').addEventListener('onblur',alertFunc,false);
    //For internet Explorer
    document.getElementById('someElementId').attachEvent('onblur',alertFunc);
    </script>


    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') });
    }
    </script>


    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!

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 thread - that expands on the basic technique, so you might find that useful.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Texas, USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Great Response form brothercake!!!

    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...

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by brothercake; 04-06-2005 at 05:43 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #5
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Texas, USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Weird Interactions in Javascript?!? Nah....

    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.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by codecoward
    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.
    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.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •