469,903 Members | 1,651 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

how to clear onClick event?

hello.

is it possible to clear onClick event?

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

--
waste
www.marti.presents.prv.pl
Jul 20 '05 #1
6 41378
waste wrote:
is it possible to clear onClick event?
eg:
<div ... onClick="someFunction()">
when I want to run someFunction() only once.


<div ... onClick="someFunction()">
<script>
function someFunction(){
this.onclick = undefined;
}
</script>
Jul 20 '05 #2
Marek Mand wrote:
waste wrote:
is it possible to clear onClick event?
eg:
<div ... onClick="someFunction()">
when I want to run someFunction() only once.


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


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/
Jul 20 '05 #3
In article <1m***************@waste.neostrada.pl>, wa***@N-O.S-P-A-
M.tlen.pl enlightened us with...
hello.

is it possible to clear onClick event?

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


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?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #4
Andrew Urquhart wrote: <%am1c.107$m56.38@newsfe1-win>
Marek Mand wrote: <snip>
<div ... onClick="someFunction()">
<script>
function someFunction(){
this.onclick = undefined;
}
</script>


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


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.
function someFunction() {
this.onclick = function() {return true;};
}


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="someFunction();this.onclick = null;">

- or (assigning a harmless dummy function):-

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

<div ... onClick="someFunction();this.onclick = retTrue;">

Richard.
Jul 20 '05 #5
The correct answer is to set it to null.

<div id="someDiv" onClick="someFunction()">click me</div>
<script>
document.getElementById( "someDiv" ).onclick = null;
</script>
waste <wa***@N-O.S-P-A-M.tlen.pl> wrote in message news:<1m***************@waste.neostrada.pl>...
hello.

is it possible to clear onClick event?

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

Jul 20 '05 #6
2004-03-03 21:00:59, na comp.lang.javascript, Nathan Sweet napisał(a):
The correct answer is to set it to null.
document.getElementById( "someDiv" ).onclick = null;


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

--
waste
www.marti.presents.prv.pl
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Andreas Knollmann | last post: by
2 posts views Thread by RobG | last post: by
6 posts views Thread by Cockroach | last post: by
5 posts views Thread by moondaddy | last post: by
7 posts views Thread by extremerep | last post: by
4 posts views Thread by sameergn | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.