bear
bear

Reputation: 11615

CKEditor insert HTML into textarea

I'm trying to insert text into CKEditor using javascript. I have currently got this script:

function quote_<?php echo $row['pid']; ?>() {
        var stringContent = $(".content_<?php echo $row['pid']; ?>").html();
         $("#wysiwyg").val("[quote=<?php echo $row['author']; ?>]" + stringContent + "[/quote]");
         CKEDITOR.instances.wysiwyg.insertHtml('[quote=<?php echo $row['author']; ?>]' + stringContent + '[/quote]');
}

<textarea name="message" style="width:100%" tabindex="3" rows="10" id="wysiwyg">
</textarea>

HTML is not being inserted into the instance 'wysiwyg', so I can't get this to work.

Any ideas?

Upvotes: 2

Views: 12937

Answers (3)

Memonic
Memonic

Reputation: 345

This worked for me:

CKEDITOR.instances.TEXTATEA_ID.insertHtml('<p> html here. </p>');

Upvotes: 1

steve_c
steve_c

Reputation: 6255

This row:

CKEDITOR.instances.wysiwyg.insertHtml('[quote={$row['author']}]' + stringContent + '[/quote]');

the single quotes around 'author' are not escaped. Try:

CKEDITOR.instances.wysiwyg.insertHtml("[quote={$row['author']}]" + stringContent + "[/quote]");

Upvotes: 2

Victor Nițu
Victor Nițu

Reputation: 1505

You can always embed your javascript code into a .php file, as it follows:

<?php

echo <<<JS

<script type='text/javascript'>
    function quote_{$row['pid']}() {
        var stringContent = $(".content_{$row['pid']}").html();
            $("#wysiwyg").val("[quote={$row['author']}]" + stringContent + "[/quote]");
        CKEDITOR.instances.wysiwyg.insertHtml('[quote={$row['author']}]' + stringContent + '[/quote]');
}
</script>
    <textarea name="message" style="width:100%" tabindex="3" rows="10" id="wysiwyg">
    </textarea>
JS;
?>

Also, you can alternatively use php files in which you keep pure (X)HTML markup, but when you come to the point where you would normally echo, just do that:

var stringContent = $(".content_"+<?= $row['pid']; ?>).html();

Upvotes: 0

Related Questions