Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
   <div class="container">
   <h1>Set cursor at end of elements demo</h1>
   <section>
      <p>Content Editable Div with Child nodes:</p>
      <div contenteditable="true" id="1" class="element" spellcheck="false">
         <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sagittis sit amet est sed volutpat. Ut eu ipsum quam.</span><span> Nam at tellus eu mauris lacinia ultrices sed at elit. Sed suscipit lectus in diam viverra, at dapibus dui lobortis.
         </span>
      </div>
      <script>let element = document.getElementById("1")</script>
      <button onclick='setCarat(element)'>
      Set Cursor at end
      </button>
   </section>
   <section>
      <p>Textarea:</p>
      <textarea id="2" class="element" spellcheck="false">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sagittis sit amet est sed volutpat.</textarea>
      <script>let element2 = document.getElementById("2")</script>
      <button onclick='setCarat(element2)'>
      Set Cursor at end
      </button>
   </section>
   <section>
      <p>Input:</p>
      <input id="3" class="element" value="Lorem ipsum dolor sit amet, consectetur adipiscing elit." spellcheck="false">
      <script>let element3 = document.getElementById("3")</script>
      <button onclick='setCarat(element3)'>
      Set Cursor at end
      </button>
   </section>
</body>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  background: #DFD8DC;
}

body, p, div, textarea, input, button {
   color: #333;
  line-height: 1.5;
  font-family: sans-serif;
}

p {
  font-weight: bold;
}

section {
  padding: 15px;
  border-bottom: 1px solid #999;
}

.container {
  width: 50%;
  margin: 0 auto;
}

h1 {
  color: #9a7f94;
  border-bottom: 2px solid #9a7f94;
}

.element {
  border: 1px solid #ccc; 
  padding: 10px; 
  font-size: 14pt;
  background: #fff;
  border-radius: 5px;
}

textarea {
  height: 50px;
  width: 100%;
}

input {
  display: inline-block;
  min-width: 500px;
}

button {
  margin-top: 10px;
  padding: 10px;
  background: #B4BAD4;
  border: none;
  border-radius: 5px;
}

button:hover {
  background: #949dc3;
}
              
            
!

JS

              
                function setCarat(element) {
    // Place cursor at the end of a content editable div
    if (element.type !== "textarea" && element.getAttribute("contenteditable") === "true") {
        element.focus()
        window.getSelection().selectAllChildren(element)
        window.getSelection().collapseToEnd()
    } else {
        // Place cursor at the end of text areas and input elements
        element.focus()
        element.select()
        window.getSelection().collapseToEnd()
    }
}
              
            
!
999px

Console