View Full Version : get cursor position +1 and -1 place

02-03-2004, 07: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, 08:13 PM
I have this so far:

var code = iView.document.selection;
var tmp = code.createRange();


newSelection = tmp.move("character", -1);
alert(newSelection );


I was hoping that the newSelection would hold the value of the letter IN FRONT of the cursor.... but it doesnt!!

help!! :rolleyes:

02-05-2004, 04:54 PM
anyone got any suggestions??! :(

Roy Sinclair
02-05-2004, 05: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, 05: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

Roy Sinclair
02-05-2004, 07: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.

Willy Duitt
02-06-2004, 12:28 AM
I was reading up on moveToPoint and ran across this (http://www.kissysoftware.com/asp/javascript/highlighting.html) which may may find helpful.


02-06-2004, 10: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 :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum