0

I have a div just like this

<div contenteditable="true" >
 Hello this is content editable.... [cursor is blinking here] and more text
</div> 

How can i set a data where cursor is blinking.

Thanks in advance.

1 Answer 1

2

You use execCommand with the insertHTML command:

document.execCommand("insertHTML", false, "the HTML to insert");

Live Example:

$("div").on("keydown", function(e) {
  if (e.ctrlKey && e.which == 81) {
    document.execCommand("insertHTML", false, "<strong>inserted</strong>");
    return false;
  }
});
<p>Put your cursor in the relevant location below, then press Ctrl+Q:</p>
<div contenteditable="true">
 Hello this is content editable....  and more text
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Sadly this doesn't seem to work on IE11. :-| For cross-browser support, you might look at Tim Down's rangy library.

Sign up to request clarification or add additional context in comments.

2 Comments

I am sure that no one uses Internet Explorer sir.
@mplungjan: Doh! And here I was going for something innocuous. Anything innocuous on OSX?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.