Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript Selection/Range Problem [modified]

JavaScript Selection/Range Problem [modified]

Scheduled Pinned Locked Moved Web Development
javascripthelpquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lea Hayes
    wrote on last edited by
    #1

    Hi, I am working on a custom rich-text editor using contentEditable. When the return key is pressed I am preventing the default action, and inserting a line break tag to ensure that the browser doesn't create a paragraph element. In IE I have this working great, but in Chrome, Safari, and FF the selection doesn't appear to update correctly. The break tag is inserted, but the input caret does not move. For test purposes I have replaced the break with a horizontal rule. Upon pressing enter, the caret should move after the HR element, but it doesn't. window.onload = function(event) { document.observe('keydown', doc_keydown.bindAsEventListener(document)); } function charCode(event) { return typeof event.which !== 'undefined' ? event.which : event.keyCode; } function doc_keydown(event) { var range; var code = charCode(event); switch (code) { case 13: // return if (document.selection) { // IE range = document.selection.createRange(); range.pasteHTML("<hr />"); range.moveEnd("character", 1); range.moveStart("character", 1); range.collapse(false); } else { // FF, Google Chrome range = window.getSelection().getRangeAt(0); range.deleteContents(); var newNode = new Element("hr"); range.insertNode(newNode); range.setStartAfter(newNode); // Doesn't work from here down. range.setEndAfter(newNode); range.collapse(false); } break; default: return true; } event.stop(); return false; }

    Some initial text - Edit Me!

    Many thanks, Lea Hayes

    modified on Friday, October 24, 2008 6:39 AM

    L 1 Reply Last reply
    0
    • L Lea Hayes

      Hi, I am working on a custom rich-text editor using contentEditable. When the return key is pressed I am preventing the default action, and inserting a line break tag to ensure that the browser doesn't create a paragraph element. In IE I have this working great, but in Chrome, Safari, and FF the selection doesn't appear to update correctly. The break tag is inserted, but the input caret does not move. For test purposes I have replaced the break with a horizontal rule. Upon pressing enter, the caret should move after the HR element, but it doesn't. window.onload = function(event) { document.observe('keydown', doc_keydown.bindAsEventListener(document)); } function charCode(event) { return typeof event.which !== 'undefined' ? event.which : event.keyCode; } function doc_keydown(event) { var range; var code = charCode(event); switch (code) { case 13: // return if (document.selection) { // IE range = document.selection.createRange(); range.pasteHTML("<hr />"); range.moveEnd("character", 1); range.moveStart("character", 1); range.collapse(false); } else { // FF, Google Chrome range = window.getSelection().getRangeAt(0); range.deleteContents(); var newNode = new Element("hr"); range.insertNode(newNode); range.setStartAfter(newNode); // Doesn't work from here down. range.setEndAfter(newNode); range.collapse(false); } break; default: return true; } event.stop(); return false; }

      Some initial text - Edit Me!

      Many thanks, Lea Hayes

      modified on Friday, October 24, 2008 6:39 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hello I have worked on the same error earlier, but unfortunatly there is no way you can disable the keypress event in Firefox, Safari, etc. You can capture KeyPress events but they can only be controlled in IE. I am sorry, i cannot help you on this topic.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups