$('.code:not(.focus)').keyup(function(){
var value = $(this).val();
//highlight the value variable then change the newline character
value.replace(/\r?\n/g,'<br/>');
document.getElementById("display_code").innerHTML = value;
//now make sure the div and textarea has the same scroll position.
});
$('#code').on('scroll', function () {
var x = $('#code').scrollTop();
$('#display_code').scrollTop(x);
console.log("Textarea: " + $('#code').scrollTop());
console.log("Div: " + $('#display_code').scrollTop());
});
$(document).delegate('#code', 'keydown', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
e.preventDefault();
var start = $(this).get(0).selectionStart;
var end = $(this).get(0).selectionEnd;
// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start)
+ "\t"
+ $(this).val().substring(end));
// put caret at right position again
$(this).get(0).selectionStart =
$(this).get(0).selectionEnd = start + 1;
}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.