Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    get cursor position +1 and -1 place

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

    eg:

    "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!!

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    I have this so far:

    Code:
    var code = iView.document.selection;
    var tmp = code.createRange();	
    	
    tmp.select();
    	
    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!!

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    anyone got any suggestions??!

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    that's exactly what I want!

    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

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #7
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was reading up on moveToPoint and ran across this which may may find helpful.

    .....Willy

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    excellent!! using that example I've mananged to get the character before and after the cursor...

    thanks guys!!

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •