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.
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?
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.
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):
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.