how to clear onClick event?

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

    #1

    how to clear onClick event?

    hello.

    is it possible to clear onClick event?

    eg:
    <div ... onClick="someFu nction()">
    when I want to run someFunction() only once.

    --
    waste

  • Marek Mand

    #2
    Re: how to clear onClick event?

    waste wrote:[color=blue]
    > is it possible to clear onClick event?
    > eg:
    > <div ... onClick="someFu nction()">
    > when I want to run someFunction() only once.[/color]

    <div ... onClick="someFu nction()">
    <script>
    function someFunction(){
    this.onclick = undefined;
    }
    </script>


    Comment

    • Andrew Urquhart

      #3
      Re: how to clear onClick event?

      Marek Mand wrote:[color=blue]
      > waste wrote:[color=green]
      >> is it possible to clear onClick event?
      >> eg:
      >> <div ... onClick="someFu nction()">
      >> when I want to run someFunction() only once.[/color]
      >
      > <div ... onClick="someFu nction()">
      > <script>
      > function someFunction(){
      > this.onclick = undefined;
      > }
      > </script>[/color]

      IIRC some javascript engines don't understand the undefined keyword.
      Perhaps the following would be better:

      function someFunction() {
      this.onclick = function() {return true;};
      }
      --
      Andrew Urquhart
      - FAQ: http://jibbering.com/faq
      - Archive: http://groups.google.com/groups?grou...ang.javascript
      - Reply: www.andrewu.co.uk/about/contact/


      Comment

      • kaeli

        #4
        Re: how to clear onClick event?

        In article <1m3kl2vq8wdoa. dlg@waste.neost rada.pl>, waste@N-O.S-P-A-
        M.tlen.pl enlightened us with...[color=blue]
        > hello.
        >
        > is it possible to clear onClick event?
        >
        > eg:
        > <div ... onClick="someFu nction()">
        > when I want to run someFunction() only once.
        >
        >[/color]

        detachEvent, maybe...tho not cross-browser.

        Myself, I'd just use a global boolean to tell me if it was run once and
        check in someFunction if it were true.

        --
        --
        ~kaeli~
        Why did kamikaze pilots wear helmets?



        Comment

        • Richard Cornford

          #5
          Re: how to clear onClick event?

          Andrew Urquhart wrote: <%am1c.107$m56. 38@newsfe1-win>[color=blue]
          > Marek Mand wrote:[/color]
          <snip>[color=blue][color=green]
          >> <div ... onClick="someFu nction()">
          >> <script>
          >> function someFunction(){
          >> this.onclick = undefined;
          >> }
          >> </script>[/color]
          >
          > IIRC some javascript engines don't understand the undefined
          > keyword. Perhaps the following would be better:[/color]

          A possible trick for guaranteeing the safe use of undefined is to create
          a property of the global object called undefined without assigning a
          defined value to it:-

          this.undefined = this.undefined; // executed inline in
          // the global context.

          Otherwise, yes the use of - undefined - as a keyword causes IE 4 to
          produce that unhelpful "undefined is undefined" error message.

          I believe that (though I haven't tested them) all browsers understand
          the assignment of - null - to the element's onclick property (and other
          event handling properties) as sufficient to disable a handler. The
          support of - null - should include all language versions.
          [color=blue]
          > function someFunction() {
          > this.onclick = function() {return true;};
          > }[/color]

          Neither of the proposed methods will work because the - someFunction -
          function is not the event handling method, it is just a function called
          by that method, so the - this - keyword will not refer to the element
          (it will refer to the global object).

          It may also not be a good idea for the execution of - someFunction - to
          form a closure, so the assignment of a harmless dummy function may be
          better not done with an inner function expression.

          <div ... onClick="someFu nction();this.o nclick = null;">

          - or (assigning a harmless dummy function):-

          <script type="text/javascript">
          function retTrue(){ return true; }
          function someFunction(){
          ...
          }
          </script>

          <div ... onClick="someFu nction();this.o nclick = retTrue;">

          Richard.


          Comment

          • Nathan Sweet

            #6
            Re: how to clear onClick event?

            The correct answer is to set it to null.

            <div id="someDiv" onClick="someFu nction()">click me</div>
            <script>
            document.getEle mentById( "someDiv" ).onclick = null;
            </script>


            waste <waste@N-O.S-P-A-M.tlen.pl> wrote in message news:<1m3kl2vq8 wdoa.dlg@waste. neostrada.pl>.. .[color=blue]
            > hello.
            >
            > is it possible to clear onClick event?
            >
            > eg:
            > <div ... onClick="someFu nction()">
            > when I want to run someFunction() only once.[/color]

            Comment

            • waste

              #7
              Re: how to clear onClick event?

              2004-03-03 21:00:59, na comp.lang.javas cript, Nathan Sweet napisa³(a):
              [color=blue]
              > The correct answer is to set it to null.
              > document.getEle mentById( "someDiv" ).onclick = null;[/color]

              thanks! this one works perfectly on Mozilla and IE (and Opera should
              manage it, too, i guess). ;>

              --
              waste

              Comment

              Working...