Fire an event from javascript!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hans

    Fire an event from javascript!

    If I for example have two anchor tags

    <a id="anchor1" onclick="....." >Link1</a>
    <a id="anchor2" onclick="....." >Link2</a>


    In a javascript function I get the id of an anchor tag and I want to execute
    the onclick event (or some other event on this element). Is it possible?

    I would like something like this
    document.getEle mentById("ancho r1").onclick

    Regards
    /Hans


  • Randell D.

    #2
    Re: Fire an event from javascript!

    Hans wrote:
    [color=blue]
    > If I for example have two anchor tags
    >
    > <a id="anchor1" onclick="....." >Link1</a>
    > <a id="anchor2" onclick="....." >Link2</a>
    >
    >
    > In a javascript function I get the id of an anchor tag and I want to execute
    > the onclick event (or some other event on this element). Is it possible?
    >
    > I would like something like this
    > document.getEle mentById("ancho r1").onclick
    >
    > Regards
    > /Hans
    >
    >[/color]

    I know there is a special object called 'this' which might prove helpful
    here - but since I'm a newbie I've only got so far in using it in the
    past - Perhaps though it might give you some direction to run with... If
    I were to try it, I would use it something like so:

    <a id="anchor1" onClick="return myfunction(this );">Link1</a>
    <a id="anchor2" onClick="return myfunction(this );">Link2</a>

    and....

    function myfunction(ourO bjectTag)
    {
    alert(ourObject Tag.name);
    // or is it
    alert(ourObject Tag.name.value) ;
    return;
    }

    Its just an idea - I hope I'm not sending you on the wrong track but I'd
    be curious of the solution when you work it out...

    randelld

    Comment

    • RobB

      #3
      Re: Fire an event from javascript!

      Hans wrote:[color=blue]
      > If I for example have two anchor tags
      >
      > <a id="anchor1" onclick="....." >Link1</a>
      > <a id="anchor2" onclick="....." >Link2</a>
      >
      >
      > In a javascript function I get the id of an anchor tag and I want to[/color]
      execute[color=blue]
      > the onclick event (or some other event on this element). Is it[/color]
      possible?[color=blue]
      >
      > I would like something like this
      > document.getEle mentById("ancho r1").onclick
      >
      > Regards
      > /Hans[/color]

      document.getEle mentById("ancho r1").onclick( );

      Since 'onclick' is an object property of Link (Link.onclick), and
      contains whatever string you assigned in HTML (inside a function
      wrapper, applied automatically), you can invoke it like any other
      function, by name. Same with handlers registered via script (including
      function pointers). Keep in mind: this doesn't synthesize the actual
      event (btw, it's CLICK, 'on' denotes the handler property) but simply
      calls the handler. The Link.click() method will do the latter, but has
      some compatibility issues.

      DOM Level 2 has expanded capabilities in this area, someone will
      doubtless mention them here...

      Comment

      • Michael Winter

        #4
        Re: Fire an event from javascript!

        RobB wrote:

        [snip]
        [color=blue]
        > DOM Level 2 has expanded capabilities in this area, someone will
        > doubtless mention them here...[/color]

        I take it you're referring to event simulation with the DOM Events
        module, which would go something like:

        var e = document.create Event('MouseEve nts'),
        a = document.links['anchor1'];

        e.initMouseEven t('click', /* Event type */
        true, /* Can bubble */
        true, /* Cancelable */
        document.defaul tView, /* View */
        1, /* Mouse clicks */
        0, /* Screen x */
        0, /* Screen y */
        0, /* Client x */
        0, /* Client y */
        false, /* Ctrl */
        false, /* Alt */
        false, /* Shift */
        false, /* Meta */
        0, /* Button */
        null); /* Related target */

        a.dispatchEvent (e);

        Yes, in theory you could do that, however it's not likely it will "do"
        anything. Any event listeners attached to the document (both capturing
        and non-capturing) will be fired, but the default action associated
        with the event is not likely to occur. This prevents a malicious
        script from faking a mouse click to open a pop-up window or submit a
        form, for example.

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        Working...