473,670 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to seperate an onclick from a onmouseover/mouseup

In a web page I have a div tag that has a onlick event registered
through the event listener. However, that same div tag also has a
onmousedown - start a drag and drop script

The problem I am having is that once the drag and drop is complete, the
mouse button is released and the onclick event is firing. I've tried
returning false from the function dealing with the mouse up and
cancelling the event with
if (e.stopPropagat ion) e.stopPropagati on();
else e.cancelBubble = true;

but the onclick is still firing. Can anyone help?

Apr 1 '06 #1
2 4707
da********@gmai l.com wrote:
In a web page I have a div tag that has a onlick event registered
through the event listener. However, that same div tag also has a
onmousedown - start a drag and drop script

The problem I am having is that once the drag and drop is complete, the
mouse button is released and the onclick event is firing.


Dave,

I have done something very simlar to this. I found that using a click
observer is not good in this situation. Better to watch for mouseup and
mousedown events.

The basics of of the approach I took was to and and remove event
listeners based on what already has occured. Something like

1) initialize the div's related JavaScript object by registering a
mousedown observer

2) in the mousedown handler add mousemove and mouseup observers

3) in the mousemove handler remove the mouseup observer. Now once a
mouse move begins you are no longer also observing for a mouse up.
Here is some code I used based on Prototype.js library for event
handling. People here will certainly caution against the use of this
library. I think this code is quite readable to show you what I did.
What I don't like about it is the unfortunate spagetti code feel of
registering and deregistering all the observers.

function Draggable(eleme nt){

// do other stuff

this.eventMouse UpAfterMove =
this.upAfterMov eHandler.bindAs EventListener(t his);
this.eventMouse Move = this.moveHandle r.bindAsEventLi stener(this);
this.eventMouse Up = this.upHandler. bindAsEventList ener(this);
this.eventMouse Down = this.downHandle r.bindAsEventLi stener(this);
this.eventKeyPr ess = this.keyHandler .bindAsEventLis tener(this);

Event.observe(t his.handle, "mousedown" , this.eventMouse Down);
}

Draggable.proto type.downHandle r = function(event) {

// do stuff

Event.observe(d ocument, "mousemove" , this.eventMouse Move);
Event.observe(d ocument, "mouseup", this.eventMouse Up);
Event.observe(d ocument, "keypress", this.eventKeyPr ess);

Event.stop(even t);
};

Draggable.proto type.moveHandle r = function(event) {

// do stuff

Event.stopObser ving(document, "mouseup", this.eventMouse Up);

Event.observe(d ocument, "mouseup", this.eventMouse UpAfterMove);

Draggable.updat eDrag(event);
Event.stop(even t);
};

Draggable.proto type._afterMove Helper = function(){

// do stuff

Event.stopObser ving(document, "mousemove" , this.eventMouse Move);
Event.stopObser ving(document, "mouseup", this.eventMouse UpAfterMove);
Event.stopObser ving(document, "keypress", this.eventKeyPr ess);
};

Draggable.proto type.upAfterMov eHandler = function(event) {

// stuff

this._afterMove Helper();
Event.stop(even t);
};

Draggable.proto type.keyHandler = function(event) {
// stuff

this._afterMove Helper();
Event.stop(even t);
};

Draggable.proto type.upHandler = function(event) {
Event.stopObser ving(document, "mousemove" , this.eventMouse Move);
Event.stopObser ving(document, "mouseup", this.eventMouse Up);
Event.stopObser ving(document, "keypress", this.eventKeyPr ess);

Event.stop(even t);
};

Apr 1 '06 #2

pe**********@gm ail.com wrote:
da********@gmai l.com wrote:
In a web page I have a div tag that has a onlick event registered
through the event listener. However, that same div tag also has a
onmousedown - start a drag and drop script

The problem I am having is that once the drag and drop is complete, the
mouse button is released and the onclick event is firing.


Dave,

I have done something very simlar to this. I found that using a click
observer is not good in this situation. Better to watch for mouseup and
mousedown events.

The basics of of the approach I took was to and and remove event
listeners based on what already has occured. Something like

1) initialize the div's related JavaScript object by registering a
mousedown observer

2) in the mousedown handler add mousemove and mouseup observers

3) in the mousemove handler remove the mouseup observer. Now once a
mouse move begins you are no longer also observing for a mouse up.
Here is some code I used based on Prototype.js library for event
handling. People here will certainly caution against the use of this
library. I think this code is quite readable to show you what I did.
What I don't like about it is the unfortunate spagetti code feel of
registering and deregistering all the observers.


ya know, I was looking at event handlers and read that a click event is
really a mousedown and a mouseup, but this trick never quite registered
in my mind. thanks a million!

Apr 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9158
by: Kevin Lyons | last post by:
Hello, Can anyone assist me with what I am trying to do with the following code (six different scenarios to try to make the functionality work correctly)? I want to always (and ONLY) display in the status bar 'Symantec Corporation' whenever anyone mouses over (onMouseOver) my image or link OR when one clicks while holding the left mouse down (onClick) on the same image or link. Upon releasing the mouse (onMouseOut), the
2
4078
by: WindAndWaves | last post by:
Hi Folk I have an image map (map of New Zealand with various areas defined using an image map). Now I am wondering if it is possible to link this "image map" to an onclick and onmouseover event. Any ideas? - TIA
6
9932
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
2
2261
by: karunakar | last post by:
Hi All In datagrid OnMouseOver iam showing some color Onmouseout also working fine . Here my problem is when ever click the pariculat row in DataGrid iam showing red color that time i am not getting any problem . when ever mouse was changed paricular row that time color is lossed "When ever click the particular Row That time i want to show that color & that time dont loss that color( when ever onMouseover changed)" Here it's not...
2
8047
by: Peter | last post by:
Hi, this is the code, and new row and new cell generated ok, but why the onclick and onmouseover doen't work? Thank you in advance! <html> <head> <script language="javascript"> function inserttable() {
4
19547
by: Amy | last post by:
I need some help. I have this table with alternate row colors. Class gray and class white. I have javascript that do highlight when mouseover row ... and onclick to select row and highlight it with another color. Also created a class called "Selected". You can only select a row at a time. My problem is, if a row is preselected, when mouseover the selected row, the selected color is screwed. Until you click on the selected row once, the...
3
4709
by: libsfan01 | last post by:
Hi all is it possible to onclick of a button in one form submit that form and also submit another form on the same page, both with the same 'action' location? or is it only possible to send one at a time? kind regards Marc
6
4889
by: Marko Mikkonen | last post by:
I'm trying to make code which does something when user clicks a row in a table. I tried a javascript code from JS-Examples.com. It works on Firefox, but not on Internet explorer 7. Here's the code: <html> <head> <!-- This file retrieved from the JS-Examples archives http://www.js-examples.com 1000s of free ready to use scripts, tutorials, forums.
4
2198
by: since | last post by:
How do I in IE prevent the onclick action from being fired when I am done dragging? have tried "window.event.cancelBubble = true", for onmouseup , onmousedown, and onmousemove handlers. The onclick sort action always seems to get fired. Any help would be greatly appeciated. The following demo is sortable and resizable. I basically don't want the onclick action to be fired if I am resizing a column. Steve <HTML> <HEAD> <style>
0
8388
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
8907
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...
1
8593
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7423
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
6218
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
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
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.