Problem with AttachEvent

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

    #1

    Problem with AttachEvent

    Hello everyone!
    I am trying to copy AttachEvents but i don't know how.
    Because innerHTML doesn't copy the AttachEvents.
    Here is an example:

    <script>
    function a(){
    alert("is changing");
    }
    </script>

    <div id="d1" name="d1">
    <select id="s1" name="s1">
    <option>A</option>
    <option>B</option>
    </select>
    </div>

    <script>
    s1.attachEvent( "onchange", a);
    </script>

    <div id="d2" name="d2">
    </div>

    <script>
    d2.innerHTML=d1 .innerHTML;
    </script>

    Can anyone help me?
  • Ivo

    #2
    Re: Problem with AttachEvent


    "PedroVasconcel os" <pmsv@zmail.p t> wrote in message
    news:e07e9b01.0 402201602.1c1cb 703@posting.goo gle.com...[color=blue]
    > Hello everyone!
    > I am trying to copy AttachEvents but i don't know how.
    > Because innerHTML doesn't copy the AttachEvents.[/color]

    No, because you end up with two elements with id="s1" and that won't work of
    course as id's need to be unique. I changed your last line into:

    if(s1) alert(s1.tagNam e); // SELECT
    d2.innerHTML=d1 .innerHTML;
    if(s1) alert(s1.tagNam e); // undefined

    HTH
    Ivo
    [color=blue]
    > Here is an example:
    > <script>
    > function a(){
    > alert("is changing");
    > }
    > </script>
    >
    > <div id="d1" name="d1">
    > <select id="s1" name="s1">
    > <option>A</option>
    > <option>B</option>
    > </select>
    > </div>
    >
    > <script>
    > s1.attachEvent( "onchange", a);
    > </script>
    >
    > <div id="d2" name="d2">
    > </div>
    >
    > <script>
    > d2.innerHTML=d1 .innerHTML;
    > </script>[/color]


    Comment

    • pmsv pmsv

      #3
      Re: Problem with AttachEvent

      Thanks for your reply but that is not the problem as you can see with
      this example:

      script>
      function merda(){
      alert("mudou");
      }
      </script>

      <div id="d1" name="d1">
      <select id="s1" name="s1">
      <option>A</option>
      <option>B</option>
      </select>
      </div>

      <div id="d2" name="d2">
      </div>

      <script>
      s1.attachEvent( "onchange",merd a);
      </script>

      <script>
      var i1=d1.innerHTML ;
      d1.innerHTML="" ;
      d2.innerHTML=i1 ;
      alert(s1.tagNam e);

      </script>

      the innerHTML does not pass the attachEvent. The innerHTML is only a
      string and if you look at it, you don't have any reference to the
      attachEvent.
      So the question remains: How can i copy AttachEvents?

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Richard Cornford

        #4
        Re: Problem with AttachEvent

        "pmsv pmsv" <pmsv@zmail.p t> wrote in message
        news:4036b8f1$0 $199$75868355@n ews.frii.net...
        <snip>[color=blue]
        >So the question remains: How can i copy AttachEvents?[/color]

        By keeping track of what you attach to which elements and then when you
        reproduce one of those elements by any means you repeat the same event
        handler attaching operations as have been applied to the original to the
        new element.

        Richard.


        Comment

        • pmsv pmsv

          #5
          Re: Problem with AttachEvent

          is that the best way? There isn't a way to ckeck the attachEvents
          without having to save history??
          Thanks

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Problem with AttachEvent

            pmsv pmsv <pmsv@zmail.p t> writes:
            [color=blue]
            > is that the best way? There isn't a way to ckeck the attachEvents
            > without having to save history??[/color]

            No. Unlike the simple "onclick" property, there is no way to get at
            the events added with attachEvent (or the non-proprietary
            addEventListene r). You must even know them in order to remove them
            with detachEvent (or removeEventList ener).

            I wouldn't use innerHTML to copy DOM nodes at all, though. I would
            rather use the DOM method cloneNode with a true argument to make
            it a deep clone.

            In your case, you don't even make a copy, you just move the nodes.
            You can do that directly:

            while(d1.hasChi ldNodes()) {
            d2.appendChild( d1.firstChild);
            }

            This has the advantage of moving the DOM nodes themselves, including
            their attached event listeners.

            It seems you are writing for IE only (using attachEvent). Otherwise,
            you should remember to initialize the variables d1 and d2.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            Working...