By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

How to use event.clientX and clientY

P: n/a
Can someone tell me why this isn't working?

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getcoords() {
document.getElementById("coord-x").value = event.clientX + "";
document.getElementById("coord-y").value = event.clientY + "";
}
</script>
</head>
<body>
<input id="coord-x" type="text">
<input id="coord-y" type="text">
<a href="#" onClick="getcoords();">Click here</a>
</body>
</html>

Isn't event.clientX and clientY supposed to return an integer value? If so,
why isn't this working? Thanks.
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"TheKeith" <no@spam.com> wrote in message
news:R_********************@giganews.com...
Can someone tell me why this isn't working?
<snip>
Isn't event.clientX and clientY supposed to return an
integer value? If so, why isn't this working? Thanks.


How are you defining "isn't working" in this context? As this (almost)
IE only code is doing exactly what it is being asked to do in IE 6.

Richard.
Jul 20 '05 #2

P: n/a

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bo*******************@news.demon.co.uk...
"TheKeith" <no@spam.com> wrote in message
news:R_********************@giganews.com...
Can someone tell me why this isn't working?

<snip>

Isn't event.clientX and clientY supposed to return an
integer value? If so, why isn't this working? Thanks.


How are you defining "isn't working" in this context? As this (almost)
IE only code is doing exactly what it is being asked to do in IE 6.

Richard.

Well, I expect it to write the x position and y position of the pointer, at
the time of my clicking the link, to the text fields. Why is this an almost
only ie code? thanks.
Jul 20 '05 #3

P: n/a
"TheKeith" <no@spam.com> writes:
Can someone tell me why this isn't working?
function getcoords() {
document.getElementById("coord-x").value = event.clientX + "";
You expect "event" to be a global value. That is an IE-specific
assumption, and it fails in most other browsers.
Solution: let "event" be an argument to the function.

You don't need the +"". Anything assigned to an input elements
value is automatically converted to a string.
<a href="#" onClick="getcoords();">Click here</a>
You forget to return false from the onclick handler, so after the
getcoords function has run, the page is reloaded (with an "#" at the
end of the address).
Change to:
<a href="" onclick="getcoords(event);return false">Click here</a>
(also passes event as argument to function. The event is available
inside the code of HTML event-handler attributes as the variable
"event", so it is ok to write "event" here).
Isn't event.clientX and clientY supposed to return an integer value?
They are supposed to *be* integer values.
Functions return, values are.
If so, why isn't this working?


"Not working" is close to useless as an error report. When asking for
help, you get the best response if you say:

1) What you do (code, what to click, etc, so we can try to recreate
the problem).
2) What is supposed to happen (hard to guess since it doesn't)
3) What really happens for you (maybe we can't recreate the problem)

In your case, the code works perfectly. Then the page is reloaded.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
"TheKeith" <no@spam.com> wrote in message
news:aZ********************@giganews.com...
<snip>
How are you defining "isn't working" in this context? As this
(almost) IE only code is doing exactly what it is being asked
to do in IE 6.
<snip>Well, I expect it to write the x position and y position of the
pointer, at the time of my clicking the link, to the text fields.
And that is exactly what it does on my IE 6 (with the caveat that the X
and Y coordinates are relative to the upper left corner of the inner
border of the client area).
Why is this an almost only ie code? thanks.


The function called by the event handling code uses the global
identifier "event" and originally only IE browsers supported a global
event object, more recently, in a bid for compatibility with IE, some
other browsers have implemented global event objects, but not all.

Richard.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.