At the first check, McFjodor's answer is what I need.
Everything seems to be fine. I modified and duplicated its code to do the same for heading text.
Modified javascript as below:
var $lastCaretDiv; $(".addHeading").click(function() { if ($lastCaretDiv) { //$lastCaretDiv.after('<div class="paragraph">Test</div>'); $lastCaretDiv.after($('<div/>', { 'html': 'Heading', 'class': 'heading' }).click(function () { $lastCaretDiv = $(this); })); } else { $('.textArea').append($('<div/>', { 'html': 'Heading', 'class': 'heading' }).click(function () { $lastCaretDiv = $(this); })); } }); $(".addParagraph").click(function() { if ($lastCaretDiv) { //$lastCaretDiv.after('<div class="paragraph">Test</div>'); $lastCaretDiv.after($('<div/>', { 'html': 'Test', 'class': 'paragraph' }).click(function () { $lastCaretDiv = $(this); })); } else { $('.textArea').append($('<div/>', { 'html': 'Test', 'class': 'paragraph' }).click(function () { $lastCaretDiv = $(this); })); } });β
Below is the completed fiddle of what I got after I answered McFjodors!
http://jsfiddle.net/aKtSt/2/
It works now, if I run into any problems with this method, I will update.
Thanks!
source share