Forked from Matt West's Pen Editing Page Elements with contenteditable.
Last active
May 28, 2016 06:46
-
-
Save dmmfll/61faa23e7367df9ada6c5c99db5c9ba6 to your computer and use it in GitHub Desktop.
Editing Page Elements with contenteditable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="page-wrapper"> | |
<h2>Toggle Editing with JavaScript</h2> | |
<button id="editorBtn" type="button">Enable Editing</button> | |
<br> | |
<div id="editor" contenteditable="true"> | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa over 140 chars | |
</textarea> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var editorBtn = document.getElementById('editorBtn'); | |
var editorArea = document.getElementById('editor'); | |
function overLimit(element) { | |
element.style.color = "red"; | |
} | |
function withinRules(element) { | |
element.style.color = "black"; | |
} | |
function verifyLength(element) { | |
var maxLength = 140; | |
var len = $(element).text().length; | |
console.log(len); | |
if (len >= maxLength) { | |
overLimit(element); | |
} else { | |
withinRules(element); | |
} | |
} | |
editorBtn.addEventListener('click', function(e) { | |
e.preventDefault(); | |
if (editorArea.isContentEditable) { | |
// Disable Editing | |
editorArea.contentEditable = 'false'; | |
editorArea.style["word-wrap"] = "break-word"; | |
editorBtn.innerHTML = 'Enable Editing'; | |
// You could save any changes here. | |
} else { | |
editorArea.contentEditable = 'true'; | |
editorBtn.innerHTML = 'Disable Editing'; | |
} | |
}); | |
$(editorArea).on('input', function() { | |
verifyLength(editorArea); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html { | |
font-family: Helvetica, Arial, sans-serif; | |
font-size: 100%; | |
background: #333; | |
color: #555; | |
-webkit-font-smoothing: antialiased; | |
} | |
#page-wrapper { | |
width: 720px; | |
background: #FFF; | |
padding: 1em; | |
margin: 1em auto; | |
min-height: 300px; | |
border-top: 5px solid #69c773; | |
box-shadow: 0 2px 10px rgba(0,0,0,0.8); | |
} | |
h1 { | |
margin-top: 0; | |
line-height: 1.3em; | |
color: #1E1E1E; | |
} | |
h2 { | |
font-weight: normal; | |
font-size: 1.4em; | |
margin: 1.5em 0 0.5em; | |
color: #1E1E1E; | |
} | |
button { | |
display: inline-block; | |
border-radius: 3px; | |
border: none; | |
font-size: 0.9rem; | |
padding: 0.4rem 0.8em; | |
background: #69c773; | |
border-bottom: 1px solid #498b50; | |
color: white; | |
-webkit-font-smoothing: antialiased; | |
font-weight: bold; | |
margin: 0 0 1em; | |
text-align: center; | |
} | |
button:hover, button:focus { | |
opacity: 0.75; | |
cursor: pointer; | |
} | |
button:active { | |
opacity: 1; | |
box-shadow: 0 -3px 10px rgba(0, 0, 0, 0.1) inset; | |
} | |
div, | |
p { | |
line-height: 1.6em; | |
} | |
section div { | |
padding: 1em; | |
background: #E6E6E6; | |
border-radius: 3px; | |
/*box-shadow: 0 1px 5px rgba(0,0,0,0.15) inset;*/ | |
outline: none; | |
border: 2px solid transparent; | |
} | |
section div:focus { | |
background: #FFF; | |
border-color: #69c773; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment