SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    contentEditable newline question.

    I'm creating a little WYSIWYG HTML editor, like the one I'm using to type this post...

    problem is... when I use a div or span with the contentEditable property set, it works just about perfectly except that when you press enter it creates a <p> instead of a <br>, which looks like crap since its a far larger break than I would like... i got over this by using a little function

    Code:
     
    if (event.keyCode == 13) { 
    	  editor.innerHTML+='<br>'
    }
    that works fine, except that now, when I press enter somwhere in the middel of some text, it jumps to the end of the text and inserts it there. I can see my problem, but I'm having a bit of a problem solving it.

    how would I get the position of the cursor and insert the linebreak there?

    or am I going about this the wrong way?
    any help would be apreciated.

    thanx
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  2. #2
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry guys.
    seems i found the solution...
    Code:
     
    if (event.keyCode == 13) { 
    	 Pos = document.selection.createRange().duplicate(); 
    	 Pos.pasteHTML('<br>') 
    }
    that seems to take care of it just fine
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne


Bookmarks

Posting Permissions

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