Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

12 of 52 people (23%) answered Yes
Recently 2 of 10 people (20%) answered Yes


How to get the LINE and COLUMN of the cursor in a TEXTAREA by press a button?

Apr 4th, 2008 20:59
ha mo, Colin Fraser, Fernando Torres,

Simply, you cannot get the column and line location, but you can get the 
mouse co-ordinates on the screen. Try this. 
 <script language="JavaScript"><!-- //hide from older browsers
//Determine the browser, naming which event and registering the event 
 if (navigator.appName == 'Netscape') {
     document.onmousemove = nnPointerPosn;            
//getting the coordinates of the pointer in Netscape, defining the the 
event handler
 function nnPointerPosn(a) {
     if (a.screenX != document.pointxy.xposn.value && a.screenY != 
document.pointxy.yposn.value) {
         document.pointxy.xposn.value = a.screenX;
         document.pointxy.yposn.value = a.screenY;
 function iePointerPosn() {
     if (window.event.x != document.pointxy.xposn.value && 
window.event.y != document.pointxy.yposn.value) {
         document.pointxy.xposn.value = window.event.x;
         document.pointxy.yposn.value = window.event.y;
 <body onMousemove="iePointerPosn()">
 <form name="pointxy">
 X posn: <input type="text" name="xposn" size="5"> Y posn: <input 
type="text" name="yposn" size="5">
This only works in NN and IE I have no idea about any other browser, I 
have not used a Mac or Unix versions of these programs, but this code 
should work on them.  Other browsers are not even considered here, so I 
am not sure if this will work on them. This code is not guarranteed, but 
it works on my pc.  
Please note that the nn and ie versions handle the pointer differently, 
so different versions of the same script are used. The iePointerMove 
function is called in the body tag. 
As far as your question is concerned, this is, of course, absolutely 
useless. You cannot hard code a response to the pointer position in a 
textarea, simply because different screen resolutions will place the  
pointer in different positions on the screen in the same textarea. But 
you can get the function to activate and do whatever you want it to do 
if you were to use an onMouseOver event.