JavaScript Triggers

JavaScript Triggers describes how you can use custom attributes to attach JavaScript events to elements.

It’s a good article and an interesting concept, but like several of the people commenting the article over at A List Apart, I’m not sure it’s worth the trouble of creating a custom DTD to be able to use custom attributes.

Posted on February 2, 2005 in JavaScript, Quicklinks

Comments

  1. Like the original poster on alistapart mentioned. ClassNames are a great (though) lesser alternative to bolt event handlers on. I’ve been playing with these for quite a while too. It keeps the source code nice and strict with all the event handling done externaly.

    Imaging having this in you code:

    <dt class="link classMouseHover toggleNextNode">
        click this to open definition 1
    </dt>
    <dd class="hideThisNode">
        Lorem ipsum dolor sit amet.
    </dd>
    

    Instead of all this:

    <dt onclick="objNode0.style.display=
    (objNode0.style.display=='none')?'block':'none';">
        click this to open definition 1
    </dt>
    <dd id="hidethis0">
        Lorem ipsum dolor sit amet.
    </dd>
    <script type="text/javascript">
        var objNode0 = document.getElementById('hidethis0');
        objNode0.style.display = 'none';
    </script>
    

    You might want a little look at my examples.

  2. February 3, 2005 by Roger Johansson (Author comment)

    Woolly: yes, that is a lot cleaner, and I’ve been using similar methods myself lately. I don’t see myself going to the trouble of making a custom DTD though. Not at the moment, anyway.

Comments are disabled for this post (read why), but if you have spotted an error or have additional info that you think should be in this post, feel free to contact me.