2

I'm trying to get the value from my SCEditor textarea, using this code:

var fbeschreibung = '';
$(function() {
    // Replace all textarea's
    // with SCEditor
    $("textarea").sceditor({
        plugins: "bbcode",
        style: "css/style.less",
        width: "100%",
        toolbar:"bold,italic,underline,strike,subscript,superscript|left,center,right,justify|size,color,removeformat|bulletlist,orderedlist,horizontalrule,emoticon",
        locale:"de" ,
        resizeEnabled:false
    });
    fbeschreibung = $('textarea').sceditor('instance').val();
    $('textarea').sceditor('instance').val('Hello [b]World![/b]');
});

I then want to send the value via AJAX:

$.post('saveprofile.php', 
   {fbeschreibung : fbeschreibung},
   function (response) {
      alert(response);
   }
);

However, I'm not able to get this to work. I haven't found find any tips in the documentation: http://www.sceditor.com/api/sceditor/val/

My variable fbeschreibung is just empty. Is there something I've done wrong?

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Juloius
  • 253
  • 1
  • 4
  • 11

3 Answers3

5

This worked for me:

$(function() {
    // Replace all textarea's
    // with SCEditor
    var fbeschreibung = $("textarea").sceditor({
        plugins: "bbcode",
        style: "css/style.less",
        width: "100%",
        toolbar:"bold,italic,underline,strike,subscript,superscript|left,center,right,justify|size,color,removeformat|bulletlist,orderedlist,horizontalrule,emoticon",
        locale:"de" ,
        resizeEnabled:false
    }).sceditor('instance');
    var value = fbeschreibung.getBody().html();
});
Paul R
  • 2,631
  • 3
  • 38
  • 72
1

I am not familiar with this particular editor, but the options (comments) I provided you may work.

var fbeschreibung = '';
$(function() {
    // Replace all textarea's
    // with SCEditor
    var editor = $("textarea").sceditor({
        plugins: "bbcode",
        style: "css/style.less",
        width: "100%",
        toolbar:"bold,italic,underline,strike,subscript,superscript|left,center,right,justify|size,color,removeformat|bulletlist,orderedlist,horizontalrule,emoticon",
        locale:"de" ,
        resizeEnabled:false
    });
    /* Try the following 3 options */

    fbeschreibung = editor.val();
    fbeschreibung = editor.getSourceEditorValue();
    fbeschreibung = editor.getWysiwygEditorValue();

    editor.val('Hello [b]World![/b]');
});
Gacci
  • 1,388
  • 1
  • 11
  • 23
  • Thanks but unfortunatly it doesnt work :/ first options: nothing happens. Second and third option gives me the error: editor.getSourceEditorValue is not a function But thank you anyway :) – Juloius Sep 02 '15 at 23:57
  • I am so sorry. I realized I made a very stupid mistake. I set the editor variable to the wrapping function and not to the editor element. I just edited, try it again! – Gacci Sep 03 '15 at 00:00
  • same result :/ and np at least we try to solve this ;) – Juloius Sep 03 '15 at 00:44
  • Try creating a fiddle, it would be easier to solve and to get help! – Gacci Sep 03 '15 at 00:48
1

I was experiencing some issues getting the JQuery code for this to work, and found that this can actually be done without using JQuery at all using:

sceditor.instance(textarea).val()

Where textarea is the textarea that your editor was created from:

let textarea = document.getElementById('my-textarea');

// the sceditor variable is created by importing the sceditor JavaScript code
// (sceditor.min.js and formats/bbcode.js)
sceditor.create(textarea, {
    format: 'bbcode',
    style: '/minified/themes/content/default.min.css'
});

function logEditorValue(){
   let textarea = document.getElementById('my-textarea');
   let value = sceditor.instance(textarea).val();

   console.log(value); // prints the value of the sceditor
}

If you have multiple SCEditors on the page, you can just use pass a different textarea to the sceditor.instance function

(More info on the Usage section on SCEditor's GitHub)

Jojodmo
  • 23,357
  • 13
  • 65
  • 107