I'm trying to use Redactor in my project. I want to add some text to the editor upon button click.
The API says you can do it like this:
function insertHtml()
{
var html = '<h3>INSERTED</h3>';
$('#redactor').redactor('insert.html', html);
}
Well, my code looks like this:
<button onclick="insertHtml();">Insert</button>
<script type="text/javascript">
function insertHtml()
{
var html = '<h3>INSERTED</h3>';
$('.redactor-box').redactor('insert.html', html);
}
</script>
Redactor editor:
<div class="redactor-box">
<ul class="redactor-toolbar" id="redactor-toolbar-0" style=
"position: relative; width: auto; top: 0px; left: 0px; visibility: visible;">
<li>
<a class="re-icon re-bold" href="#" rel="bold" tabindex="-1"></a>
</li>
<li>
<a class="re-icon re-italic" href="#" rel="italic" tabindex=
"-1"></a>
</li>
<li>
<a class="re-icon re-deleted" href="#" rel="deleted" tabindex=
"-1"></a>
</li>
<li>
<a class="re-icon re-unorderedlist" href="#" rel="unorderedlist"
tabindex="-1"></a>
</li>
<li>
<a class="re-icon re-orderedlist" href="#" rel="orderedlist"
tabindex="-1"></a>
</li>
<li>
<a class="re-icon re-outdent" href="#" rel="outdent" tabindex=
"-1"></a>
</li>
<li>
<a class="re-icon re-indent" href="#" rel="indent" tabindex=
"-1"></a>
</li>
<li>
<a class="re-icon re-image" href="#" rel="image" tabindex="-1"></a>
</li>
<li>
<a class="re-icon re-file" href="#" rel="file" tabindex="-1"></a>
</li>
<li>
<a class="re-icon re-link" href="#" rel="link" tabindex="-1"></a>
</li>
<li>
<a class="re-icon re-horizontalrule" href="#" rel="horizontalrule"
tabindex="-1"></a>
</li>
<li>
<a class="re-icon re-mathematik redactor-btn-image" href="#" rel=
"mathematik" tabindex="-1"></a>
</li>
</ul>
<div class="redactor-editor redactor-placeholder" contenteditable="true"
dir="ltr" style="min-height: 300px;">
<p></p>
</div>
<textarea class="redactor-box" cols="40" data-redactor-options=
"{"lang": "en", "fileUpload": "/redactor/upload/file/", "imageUpload": "/redactor/upload/image/"}"
dir="ltr" id="id_body" name="body" placeholder="Vsebina vprašanja.." rows=
"10" style="display: none;">
Error I'm getting:
Uncaught TypeError: Cannot read property 'insert' of undefined(anonymous function) @ redactor.js:47n.extend.each @ jquery-2.1.4.min.js:2n.fn.n.each @ jquery-2.1.4.min.js:2$.fn.redactor @ redactor.js:39insertHtml @ (index):366onclick @ (index):205
What am I doing wrong?
I'm going to assume it's because the example uses a jquery id lookup, which returns a single element, and your implementation is using a jquery class lookup which returns an array of elements.
Either use:
$('.redactor-box')[0]
or change the element's class in question to an id, and use$('#redactor-box')
.