473,804 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


<div ... onClick="someFu nction()">
<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="someFu nction()">
when I want to run someFunction() only once.


<div ... onClick="someFu nction()">
<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.neost rada.pl>, wa***@N-O.S-P-A-
M.tlen.pl enlightened us with...
hello.

is it possible to clear onClick event?

eg:
<div ... onClick="someFu nction()">
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="someFu nction()">
<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="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.
Jul 20 '05 #5
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 <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="someFu nction()">
when I want to run someFunction() only once.

Jul 20 '05 #6
2004-03-03 21:00:59, na comp.lang.javas cript, Nathan Sweet napisa³(a):
The correct answer is to set it to null.
document.getEle mentById( "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7032
by: Andreas Knollmann | last post by:
Hi, I create an object like this: var cell = document.createElement("td"). It doesn't have to be cell. I want this cell to use the onclick event. What doesn't work in the IE as well as with Mozilla is: cell.onclick = "whatever()";
2
18582
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } Which is called statically by: <button onclick="doClick(event,this);">Click me</button>
17
4886
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')' document.getElementById('mySpan').onclick = oncl
6
9945
by: Cockroach | last post by:
Hello, I have a problem where the onClick of a table row will activates a window.location event, and inside a cell in that row, an image onClick event shows/hides a div. The problem is that when you click on the image, it briefly shows the div, and then reloads the page to the window.location url. Is there a way of preventing the onClick of the row (<tr>) from doing
4
2271
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML source:
5
2596
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below is the code for this. the onclick event calls a javascript function which I put an alert in the firt line to tell me if its working. It does work in IE. Any ideas on how to get netcrap... oops, I'm sorry, netscape to fire the onclick event? ...
5
5852
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. Thanks kai
7
2797
by: extremerep | last post by:
My task is to change the value of a button and then make it functional with the onClick handler. Changing the value to "Play Again" works, but making the onClick work accordingly does not. The following is a snippet of the script. What is keeping it from working? function displaycards1(){ document.form1.reveal1.value="Play Again"; document.form1.reveal1.onClick="setcards();"; } </script>
4
13570
by: sameergn | last post by:
Hi, I have an image in my HTML form which has onclick() handler. There is also a submit button and a text box. Whenever text box has focus and user presses enter, the onclick() event of image is fired with event.keyCode as undefined. I was expecting that the form would get submitted. I tried returning from onclick() handler if keyCode is null, but the
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.