When I'm typing in my code tag and it updates the context with the script below, it keeps moving my cursor to the start of the code box. How can I fix it?
function HastTagLocation(Controll) {
var controller = $('.' + Controll);
controller.keyup(function() {
// Get the code element.
var e = $(this);
// Get the text content.
var text = e.text();
// Replace the matches and wrap them with span tag.
var text = text.replace(/(\![a-zA-Z]*)/g, '<span class="mark">$1</span>');
// Set the html of the original element.
e.html(text);
});
};
$(document).ready(function() {
HastTagLocation('form-control');
});
.hash {
background-color: #808080;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<code class="form-control" id="test" contenteditable="true" style="height: 100%; min-height: 100px"></code>
How is it possible to solve this problem?
Thanks for your time and help.
UPDATE
The solution is not to get the cursor jump to the end of text but to make the cursor jump back to were I last wrote something