View Full Version : get cursor position +1 and -1 place
02-03-2004, 06:03 PM
does anyone know how to get the current cursor position in a paragraph of text, and possibly apply formatting to the character -1 of the position and +1 of the position...??
"This is my sentance"
if I place the cursor after the "s" in sentance..... and based on what key I press, eg: DEL or BACKSPACE I want to apply formatting...
if DEL key pressed, make the letter in front of the mouse position red
if BACKSPACE pressed, make the letter behind the cursor RED.
- I already have the code to get the keypresses, but dont have the code to get the letter and apply formatting to it!!!!
any help is much appreciated!! :)
02-03-2004, 07:13 PM
I have this so far:
var code = iView.document.selection;
var tmp = code.createRange();
newSelection = tmp.move("character", -1);
I was hoping that the newSelection would hold the value of the letter IN FRONT of the cursor.... but it doesnt!!
02-05-2004, 03:54 PM
anyone got any suggestions??! :(
02-05-2004, 04:48 PM
There's a basic problem with what you're asking, you want to change the formatting on individual characters but in html pages you can only apply formatting to complete elements. In order to do what you want you'd need to extract the complete content of what ever element on the page the cursor was contained in, find the position of the cursor within the extracted content, insert new elements (like <span> tags) to contain the "character before" and the "character after", replace the extracted content with your modified content and then apply formatting to that new content (actually you can apply the formatting as you insert the <span>s).
Of course this assumes you're not planning on supporting some antique browser like Netscape 4.x where things like this are simply impossible anyway.
02-05-2004, 04:58 PM
that's exactly what I want! :thumbsup:
The browser isnt a problem - the page is running in an iFrame - so IE5.5+ will be used in all cases.
any ideas how to actually do what you described?!
thanks for any help :p
02-05-2004, 06:55 PM
Your "event" object for the select will point at the parent object containing the selected text, use innerHTML to get the content and then search that content for the selected text. Once you've got the index to the select text you can insert some <span> tags at the appropriate places and then use the innerHTML property to write the new text back to the page.
02-05-2004, 11:28 PM
02-06-2004, 09:48 AM
excellent!! using that example I've mananged to get the character before and after the cursor...
thanks guys!! :thumbsup:
You can see the example here: http://www.recaf.net/tmp/test/editor_hw.htm
the problem I now have is that when you press the DEL key, it jumps forwards two chars when it should only move one! and the backspace key colourises the char behind which is right - but needs to move one character back!!
Any thoughts on that?? thanks :)
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.