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: /
My variable fbeschreibung
is just empty. Is there something I've done wrong?
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./api/sceditor/val/
My variable fbeschreibung
is just empty. Is there something I've done wrong?
-
For starters, use a selector that selects a single element. E.g.
$('textarea').first()
. – brunoais Commented Sep 3, 2015 at 8:03 - See the docs: sceditor./api/sceditor/val – Avatar Commented Dec 16, 2017 at 15:46
3 Answers
Reset to default 5This 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();
});
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)
I am not familiar with this particular editor, but the options (ments) 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]');
});