1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.
  2. Better Analytics for WordPress Get It Free

Cursor position line number in Textarea in firefox.

Discussion in 'JavaScript' started by kshetty, Jun 12, 2007.

  1. #1
    Hi All,

    I am new into java programming. Currently I am working on a java application, in which I have a textarea. The user at any moment can change the font size and type in the textarea. Now here I need to get the cursor line position i.e. the line number in which cursor is situated. I am able to get the line number for IE using boundingTop, boundingLeft property in javascript. But the same property if I am not wrong is not supported in Firefox. I have also tried solution like searching number of \n in the textarea value, but since I am using auto wrap it does not add \n for new line text. Can anyone please help me with this, some idea regarding the same.:)

    Thanks & Regards,

    Kishore.
     
    kshetty, Jun 12, 2007 IP
  2. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What do you want to do once you know the cursor position? Will the user be "selecting" text? Will you insert text at the current position? What?
     
    Mike H., Jun 13, 2007 IP
  3. kshetty

    kshetty Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Mike,

    Thanks for your reply. Basically I am working on a chat window. Here textarea is used by user to type message and send it. Now I want to give a functionality to user where in the user can look previous messages sent by using up/down arrow. Now if the message is multiline that is more than one line the previous message should be seen only if cursor is in first or last line of the message. And one more thing, I am using auto wrap hence '\n' won't be added for new line. I am able to get line number of cursor in IE by using boundingleft property which is not supported in Firefox. Is there a way of getting the line number of the cursor in firefox and also total number of lines in textarea.

    Thanks & Regards,

    Kishore.
     
    kshetty, Jun 13, 2007 IP
  4. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Kishore:

    Well, I don't know if the following code will help in the situation you described, but it does return the line number, and character number, of the position of the cursor inside a textarea.

    You also may want to look at the free AJAX/PHP Chat application I wrote, that is listed here:

    http://www.freewarefiles.com/program_5_53_23396.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <script type="text/javascript">
    
    	function getCursor(nBox){
    
    		var cursorPos = 0;
             	if (document.selection)
    			{ 
    	         	 nBox.focus();
                 	 	 var tmpRange = document.selection.createRange();
      	         	 tmpRange.moveStart('character',-nBox.value.length);
            	 	 cursorPos = tmpRange.text.length;
         			}
            	else 	{
    		 	 if (nBox.selectionStart || nBox.selectionStart == '0')
    				{
    				 cursorPos = nBox.selectionStart;
    				}
    			}
    		var lineNumber =  parseInt(cursorPos/25) + 1;  // 25 is the number of textarea columns
    		alert('Line Number: ' + lineNumber);
    		return cursorPos;
      	}
    
    </script>
    </head>
    <body>
    	<form action="">
    		<textarea name='area1' cols='25' rows='10'>That's one small step for man, one giant leap for mankind.</textarea>
    		<br><br>
    		<input type='button' value='Get Cursor Position' onclick="alert(getCursor(this.form.area1))">
    	</form>
    </body>
    </html>
    
    Code (markup):
     
    Mike H., Jun 14, 2007 IP
  5. kshetty

    kshetty Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi Mike,
    Thank you very much for your reply. I will have to make some changes in the code you mentioned as you are taking 25 as fixed columns, in my case user can change the font size at any moment also change the width of textarea so I will have to take that into consideration. And also user can hit enter button and goto next line. I will come back on this once I check the same.

    Thanks % Regards,

    Kishore.
     
    kshetty, Jun 14, 2007 IP