event trickery

Discussion in 'Javascript' started by J. Baute, May 17, 2005.

  1. J. Baute

    J. Baute Guest

    Hi,

    I'm trying to define/create a custom "event" for an input control (or
    whatever really).

    What I'm looking for is to define an event which contains some
    javascript code that I can later on call from other code. This could
    be used to create some sort of form field validation on submitting the
    form for instance.

    Currently I've found a way to make this work, but I'm wondering if
    there isn't a cleaner or better way to do this.

    --- start ---
    <HTML>
    <HEAD>

    <SCRIPT LANGUAGE=javascript>
    <!--

    function test(o) {
    alert("test");
    // create "event handler" in input control using the evil eval
    statement
    eval("o.onSomething = function anonymous(){" +
    o.getAttribute("onSomething") + "};");
    o.onSomething(); // call new event handler
    }

    function doSomething(){
    alert("something");
    }

    //-->
    </SCRIPT>

    </HEAD>
    <BODY>

    <input type="button" value="test" onSomething="doSomething();"
    onclick="test(this);">

    </BODY>
    </HTML>
    --- end ---
     
    J. Baute, May 17, 2005
    #1
    1. Advertising

  2. J. Baute wrote:

    > I'm trying to define/create a custom "event" for an input control
    > (or whatever really).


    Events are defined by the DOM, so unless you write a UA, and a DOM
    including an appropriate ECMAScript language binding of your own,
    forget it. The approach you presented is not even Valid HTML, let
    alone host objects do not have to provide a mechanism that actually
    adds properties.


    PointedEars
     
    Thomas 'PointedEars' Lahn, May 24, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    932
    Purvi T
    Oct 19, 2004
  2. G*rd*n

    Registry trickery?

    G*rd*n, Nov 6, 2004, in forum: HTML
    Replies:
    13
    Views:
    902
  3. array copy via "struct-trickery"

    , Aug 28, 2007, in forum: C Programming
    Replies:
    8
    Views:
    348
    Keith Thompson
    Aug 31, 2007
  4. Paolo Nusco Perrotta

    Trickery in the ancestors chain

    Paolo Nusco Perrotta, Jan 24, 2007, in forum: Ruby
    Replies:
    9
    Views:
    155
  5. Steven D'Aprano
    Replies:
    0
    Views:
    180
    Steven D'Aprano
    Oct 3, 2012
Loading...

Share This Page