MyBB Community Forums

Full Version: Clearing (sc)editor's textarea
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I found out that I can add text to the sceditor's texterea with the jQuery method:

$('#message').sceditor('instance').insert(textvariable);

where textvariable is the variable containing the text which is appended to the editor's textarea.

I tested it and it works. I wonder, however, whether there is also a method with which I can clear the text area contents.


EDIT
This is a solving record, I think. I just found out that the method to use is:


$('#message').sceditor('instance').val("");

I leave it here , just in case someone else is looking for it.


This gives me the chance to broaden my question, however.

The above works for sceditor only. How could I achieve the same for all editors?


EDIT 2
The normal JQuery solution would be:

$('#message').val(textvariable);

and I also found:
$('textarea#message').val(textvariable);


I tried this, together with normal javascript methods (although I do not know whether these can be mixed with jQuery), but none of these worked!!
(2015-07-31, 08:39 PM)Ad Bakker Wrote: [ -> ]The normal JQuery solution would be:

$('#message').val(textvariable);

and I also found:
$('textarea#message').val(textvariable);

Those are exactly the same thing.. Except that you're using a more specific CSS selector in the 2nd case.

(2015-07-31, 08:39 PM)Ad Bakker Wrote: [ -> ]I tried this, together with normal javascript methods (although I do not know whether these can be mixed with jQuery), but none of these worked!!

It would only work with no custom editor used (regular <textarea>) or with editors that don't change the input field to their own HTML (which are a rarety).

(2015-07-31, 08:39 PM)Ad Bakker Wrote: [ -> ]The above works for sceditor only. How could I achieve the same for all editors?

Not sure what "all editors" are, but you'd have to detect the editor used currently on the page and implement codes for each case separately, using their own text insertion/reset methods. For example, this snippet in footer can possibly be used to make sure it's SCEditor:
<script type="text/javascript">
	$(document).ready(function() {
		var is_sceditor = false;		

		if(typeof jQuery.fn.sceditor !== 'undefined') { 
			var test = $('#message').sceditor('instance');
			if(typeof test !== 'undefined')
			{
				test.val('Hello [b]World![/b]');
				is_sceditor = true;
			}
		}
		
		if(is_sceditor === false)
		{
			alert('#message does not use SCEditor');
			// other detection code
		}
	});
</script>
(2015-08-01, 01:36 PM)Destroy666 Wrote: [ -> ]Not sure what "all editors" are, but you'd have to detect the editor used currently on the page and implement codes for each case separately, using their own text insertion/reset methods.

OK, clear. Thanks for the answers.

I already thought that it had to be something that the editor does with the textarea HTML of the message. I tried it out at the subject field and that worked (jQuery and javascript).

I myself only use sceditor, but you read sometimes that members prefer another one (like CKEditor). I did not read of the use of TineMCE yet, which seems quite popular. For 1.6 there was a plugin, but I did not read abot one for 1.8.

I'll use the script to at  least have a message when this is the cause of failure.