473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.stopPropagation) e.stopPropagation();
else e.cancelBubble = true;

but the onclick is still firing. Can anyone help?

Apr 1 '06 #1
2 4644
da********@gmail.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(element){

// do other stuff

this.eventMouseUpAfterMove =
this.upAfterMoveHandler.bindAsEventListener(this);
this.eventMouseMove = this.moveHandler.bindAsEventListener(this);
this.eventMouseUp = this.upHandler.bindAsEventListener(this);
this.eventMouseDown = this.downHandler.bindAsEventListener(this);
this.eventKeyPress = this.keyHandler.bindAsEventListener(this);

Event.observe(this.handle, "mousedown", this.eventMouseDown);
}

Draggable.prototype.downHandler = function(event){

// do stuff

Event.observe(document, "mousemove", this.eventMouseMove);
Event.observe(document, "mouseup", this.eventMouseUp);
Event.observe(document, "keypress", this.eventKeyPress);

Event.stop(event);
};

Draggable.prototype.moveHandler = function(event){

// do stuff

Event.stopObserving(document, "mouseup", this.eventMouseUp);

Event.observe(document, "mouseup", this.eventMouseUpAfterMove);

Draggable.updateDrag(event);
Event.stop(event);
};

Draggable.prototype._afterMoveHelper = function(){

// do stuff

Event.stopObserving(document, "mousemove", this.eventMouseMove);
Event.stopObserving(document, "mouseup", this.eventMouseUpAfterMove);
Event.stopObserving(document, "keypress", this.eventKeyPress);
};

Draggable.prototype.upAfterMoveHandler = function(event){

// stuff

this._afterMoveHelper();
Event.stop(event);
};

Draggable.prototype.keyHandler = function(event){
// stuff

this._afterMoveHelper();
Event.stop(event);
};

Draggable.prototype.upHandler = function(event){
Event.stopObserving(document, "mousemove", this.eventMouseMove);
Event.stopObserving(document, "mouseup", this.eventMouseUp);
Event.stopObserving(document, "keypress", this.eventKeyPress);

Event.stop(event);
};

Apr 1 '06 #2

pe**********@gmail.com wrote:
da********@gmail.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
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...
2
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...
6
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...
2
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...
2
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...
4
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...
3
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...
6
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:...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.