I am developing a text editor which allows users to input HTML text boxes in the document. e.g if the user writes ‘Hello world’, places the caret between ‘hello’ and ‘world’ and clicks the text box button, I need to place a text box control at the current caret position. Is this possible?
Currently I can place the text box at the end which is quite simple as I just create a textbox in JavaScript and append it to the <body> element. The thing that I dont know is how can we place HTML controls in between strings.
We can insert text at cursor position by using execCommand('insertHTML',..) but can we use this to insert HTML tags? Below is the HTML code that I am trying to achieve:
Before:
<iframe>
..
<body>
"hello world"
</body>
</iframe>
After clicking the textbox button it sholud be somewhat like this:
<iframe>
..
<body>
"hello <input type"text" /> world" //NOTE: this wont work as it will just print it with the tags nad nt exec it
</body>
</iframe>
You can do this using
document.execCommand("InsertInputText")in IE anddocument.execCommand("InsertHTML")in other browsers.Live demo: http://jsfiddle.net/DymzW/
Code: