473,574 Members | 2,341 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 4683
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
9153
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...
2
4076
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
9928
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...
2
2259
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...
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
19544
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...
3
4708
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
4887
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,...
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...
0
7741
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...
0
8085
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. ...
0
8259
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...
1
7838
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...
0
8126
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6491
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...
0
3763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1090
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...

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.