473,473 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Detect click inside div mozilla

Hi,

I have a site here: http://www.atom.no/christiane/defaul...&RESOURCE_ID=2

which uses divs to create a outlook like calendar interface. When you
click at a specific day, a specific hour, it will suggest to create a
booking at the clicked day.

I was having a load of troubble trying to find exactly where inside
the div element the mouse was clicked - untill I found out that ie
supported a property called simply event.y - that one seems to return
the correct y corrdinate of the click inside the div no matter the
position of the div on the page -

I would like the calendar to work on firefox aswell, and that's why I
am asking you for help - I simply wonder how I can get the y position
of the mouse inside each weekday div (from 0 to the height of the
div.)

Thanks in advance-
Simon
Jul 23 '05 #1
4 9266
Simon Pedersen wrote:
Hi,

I have a site here: http://www.atom.no/christiane/defaul...&RESOURCE_ID=2

which uses divs to create a outlook like calendar interface. When you
click at a specific day, a specific hour, it will suggest to create a
booking at the clicked day.

I was having a load of troubble trying to find exactly where inside
the div element the mouse was clicked - untill I found out that ie
supported a property called simply event.y - that one seems to return
event.x and event.y are IE only.
the correct y corrdinate of the click inside the div no matter the
position of the div on the page -
You can do what you are trying to far more reliably if you create an
element (say a div or span) for each day, then put an onclick on it
that tells you what 'day' was clicked on.

I would like the calendar to work on firefox aswell, and that's why I
am asking you for help - I simply wonder how I can get the y position
of the mouse inside each weekday div (from 0 to the height of the
div.)


Have a look here:

<URL:http://www.quirksmode.org/viewport/compatibility.html>

or a quick 'n dirty implementation of the above:

<style type="text/css">
div {border: 1px solid red; color: black; background-color: #9999DD;
width: 20em; height: 40em;}
</style>

<script type="text/javascript">
function sayLoc(e) {
e = e || window.event;
var tgt = e.target || e.srcElement;

// Get top lef co-ords of div
var divX = findPosX(tgt);
var divY = findPosY(tgt);

// Workout if page has been scrolled
var pXo = getPXoffset();
var pYo = getPYoffset();

// Subtract div co-ords from event co-ords
var clickX = e.clientX - divX + pXo;
var clickY = e.clientY - divY + pYo;

alert('Co-ords within div (x, y): '
+ clickX + ', ' + clickY);
}

function findPosX(obj) {
var curleft = 0;
if (obj.offsetParent) {
while (obj.offsetParent) {
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
} else if (obj.x) {
curleft += obj.x;
}
return curleft;
}

function findPosY(obj) {
var curtop = 0;
if (obj.offsetParent) {
while (obj.offsetParent) {
curtop += obj.offsetTop
obj = obj.offsetParent;
}
} else if (obj.y) {
curtop += obj.y;
}
return curtop;
}

function getPXoffset(){
if (self.pageXOffset) { // all except Explorer
return self.pageXOffset;
} else if (document.documentElement
&& document.documentElement.scrollTop) {// Explorer 6 Strict
return document.documentElement.scrollLeft;
} else if (document.body) { // all other Explorers
return document.body.scrollLeft;
}
}

function getPYoffset(){
if (self.pageYOffset) { // all except Explorer
return self.pageYOffset;
} else if (document.documentElement
&& document.documentElement.scrollTop) {// Explorer 6 Strict
return document.documentElement.scrollTop;
} else if (document.body) { // all other Explorers
return document.body.scrollTop;
}
}
</script>

<div onclick="sayLoc(event);"></div>
Jul 23 '05 #2
Thanks-

although at first somewhat confusing (i had been fiddling with this
code earlier) - it turned out quite all right. Now the solution works
both in Firefox and IE.

Kind Regards,
Simon

Jul 23 '05 #3
ni****@gmail.com wrote:
Thanks-

although at first somewhat confusing (i had been fiddling with this
code earlier) - it turned out quite all right. Now the solution works
both in Firefox and IE.

Kind Regards,
Simon


I was suggesting that you do something like this:

<style type="text/css">
..aDay {border: 1px solid red; width: 2em; height: 2em;
position: absolute; left: 0em;}
</style>

<script type="text/javascript">
function sayID(e){
e = e || window.event;
var tgt = e.target || e.srcElement;
alert('You clicked on ' + tgt.id);
}
</script>

<div style="position:relative;" onclick="
sayID(event);">
<div style="top: 0em;" id="Mon" class="aDay">Mon</div>
<div style="top: 2em;" id="Tue" class="aDay">Tue</div>
<div style="top: 4em;" id="Wed" class="aDay">Wed</div>
<div style="top: 6em;" id="Thu" class="aDay">Thu</div>
</div>
That's very rough, but you can see it's vastly simpler and more
reliable than trying to guess what was clicked on by the x,y
coordinates. It works in IE & Firefox of course.

It also allows the user to specify any font size and everything still
works (try setting your browser to larger/smaller fonts).

--
Fred
Jul 23 '05 #4
Hi Fred,

I understand your point. If you check out the code of the component
however, I think you will see the logic in which makes the present
solution work - also would work when font sizes are larger.

However- I am quite a newbie when it comes to dhtml, so there might be
aspects I didn't totally understand.

Thanks for your time,

- Simon

Jul 23 '05 #5

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

Similar topics

0
by: selowan | last post by:
Hi, In VB6 Pro SP5, I have a form that contains an MSFlexGrid and a few other textboxes and comboboxes. I am using the technique from MSDN article Q241355, which describes how to tab and edit in...
2
by: Ringwraith | last post by:
Hello! How to detect in Python that some click action was performed in some module imported by my main module? Let's assume I have a tree.py module that implements all functionality of a tree...
1
by: ripken95 | last post by:
I connect to the internet through ADSL. I want to write the web page which can detect the connection status with javascript. This detection is like the signal detection in the mobile phone. If the...
1
by: Mat | last post by:
How can I detect when a link has been clicked but the new page is still in the process of loading? The document.location.href property still displays the current location (understandably) not the...
2
by: Yurij Nykon | last post by:
Hi all. How can I detect the version of Flash-Plugin installed in Internet Explorer? In Netscape i can do this with following java script navigator.plugins.description But it doesn't works...
10
by: Noozer | last post by:
Is it possible to detect where on a page the click occurred when the OnClick event of the BODY tag is fired? Thx
11
by: CW | last post by:
I have message entry screen that's causing me a bit of an issue. At the moment, there are 2 buttons, one is used to send message to another user (btnSend) and another is used to send messages to...
3
by: Asterbing | last post by:
Since the "on fly addition..." thread has taken another direction, I'm opening a new one to be more explicit and recenter the subject. Well, the subject is to detect when a document is well...
1
by: teo | last post by:
hallo In a DataGridView how/where may I detect the click on the header of a column versus the click on a cell "inside" the grid ? I tried by using the
0
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...
0
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...
0
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.