Abu Ayyub
Abu Ayyub

Reputation: 411

populate ckeditor form with jquery

i am trying to populate form from mysql data with Json parse in Jquery, everything was ok except the "CKEDITOR Field / textarea" always blank, this is my code

function populateForm(form, dataJSON)
{
    var data = $.parseJSON(dataJSON);
    $.each(data, function(key, value) {
        var $ctrl = $("#"+form).find('[id='+key+']');
        if ($ctrl.is('select')){
            $('option', $ctrl).each(function() {
                if (this.value == value)
                    this.selected = true;
            });
        }
        else if ($ctrl.is('textarea')) {
            $ctrl.val(value);
        }
        else {
            switch($ctrl.attr("type")) {
                case "email":
                case "text":
                case "hidden":
                    $ctrl.val(value);
                    break;
                case "checkbox":
                    if (value == 'on')
                        $ctrl.prop('checked', true);
                    else
                        $ctrl.prop('checked', false);
                    break;
            }
        }
    });
}

How to solve this?

Upvotes: 0

Views: 284

Answers (1)

Rik Lewis
Rik Lewis

Reputation: 749

The CKEditor creates a widget that shows the text, which is separate from the original textarea. Rather than updating the textarea yourself, if the CKEditor has already been initialised, you need to add the text to the widget programmatically, like this...

CKEDITOR.instances.IDofEditor.insertText(value);

Upvotes: 1

Related Questions