<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="http://rangy.googlecode.com/svn/trunk/dev/rangy-core.js"></script>
<script src="http://rangy.googlecode.com/svn/trunk/dev/rangy-textrange.js"></script>
<title>Rangy with replacement of innerHTML</title>
</head>
<body>
<div id="area" contentEditable="true"; onkeyup="formatText();"></div>
<br>
<label>Cursor Position:</label>
<span id="cursorPos"></span>
<br><br>
<label>UserInput Length:</label>
<span id="userInputlength"></span>
<br><br>
<label>UserInput2 Length:</label>
<span id="userInput2length"></span>
</body>
</html>
#area {
margin-left: 0px;
width:500px;
white-space: pre-wrap; /* CSS3 */
white-space: pre-wrap; /* Firefox */
white-space: wrap; /* Opera <7 */
white-space: pre-wrap; /* Opera 7 */ word-wrap: break-word;
font: 62.5%"Trebuchet MS", sans-serif;
min-height:2.4em;
font-family: monospace;
font-size: 2em;
border: 2px solid #bbb;
border-radius: 0.5em;
padding: 0.2em 0.3em 0.2em 0.4em;
background-color: white;
outline: none; /* removes dotted outline */
}
var sel;
var savedSel;
var userInput;
var cursorPos;
function formatText() {
el = document.getElementById("area");
userInput = el.textContent || el.innerText;
sel = rangy.getSelection();
var range = sel.getRangeAt(0);
// Get the text preceding the selection boundaries and then remove whitespace to get the character offsets
var rangePrecedingBoundary = range.cloneRange();
rangePrecedingBoundary.setStart(el, 0);
var selEndOffset = rangePrecedingBoundary.text().replace(/\s+/g, '').length;
rangePrecedingBoundary.setEnd(range.startContainer, range.startOffset);
var selStartOffset = rangePrecedingBoundary.text().replace(/\s+/g, '').length;
rangePrecedingBoundary.detach();
//strip spaces from userInput
userInput2 = userInput.replace(/\s+/g, '');
//apply style
var newHTML = "<span style='color: green'>" + userInput2 + "</span>";
//replace innerHTML of DIV
el.innerHTML = newHTML;
/* debug DIVs */
$('#cursorPos').text(selStartOffset + " - " + selEndOffset);
$('#userInputlength').text(userInput.length);
$('#userInput2length').text(userInput2.length);
/* end debug */
range.selectCharacters(el, selStartOffset, selEndOffset);
sel.setSingleRange(range);
}
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |