Netshine Software Limited







Home arrow Developer Blog Make Text BiggerMake Text SmallerReset Text Size

Dec 10
2008

Capturing content from nicEdit when form is submitted by Javascript

Posted by netshine in wysiwygonsubmitnicEditJavascripthtmlforms

nicEdit is a very lightweight wysiwyg HTML editor, distributed under the MIT license (which means that as long as the copyright info remains intact, you can do pretty much anything with it, including distribute it with a commercial application). There are a couple of drawbacks to nicEdit - first, it does not produce standards compliant HTML. If it did, it would not be anywhere near as lightweight, so that's a bit of a tradeoff (it relies on the browser's own HTML editing features, so if you use a decent browser you should get pretty good results). The other main problem I found though, is that if you have a form containing a nicEdit editor, and submit the form using Javascript ie. form.submit(), the content of the editor is not posted back to the server!

If you would like to know why that is, and what you can do about it, read on...

The root cause of the problem is that the Javascript onsubmit event for form elements is only triggered by a user clicking on a submit button. Most browsers do not trigger the onsubmit function when a form is submitted by Javascript. This is insane, counter-intuitive, and a pain in the neck, but we have to live with it.

The normal solution to the problem then, is to call whatever code the onsubmit event calls before submitting the form. That's all very well if you are writing the code yourself, but if you are using nicEdit, and your knowledge of Javascript is a bit patchy, you could find yourself a bit stuck. Even looking at the generated source of your form will not show you what is happening in the form's onsubmit event because nicEdit programatically adds an event listener to the form. So I had to delve into the nicEdit source code to find out what to call.

Assuming you have instantiated a nicEdit editor and attached it to a textarea on your form using a piece of code similar to this:

var editor1 = new nicEditor({iconsPath : '/path/to/nicEdit/nicEditorIcons.gif', fullPanel : true}).panelInstance('text_area_id');

...you would then need to do this to capture the HTML into your textarea before submitting the form via javascript:

for(var i=0;i<editor1.nicInstances.length;i++){editor1.nicInstances[i].saveContent();}

I used a for loop because nicInstances is an array. As far as I can tell, there will only ever be one element in that array when nicEdit is instantiated as shown above, but just to be sure I iterate through all elements. After calling that saveContent method, the HTML should be copied into the textarea and therefore available in the postback. Of course if you have more than one editor on a page, you would have to repeat it for each editor.



Comments (7)Add Comment
NicEdit securimage Captcha AJAX
written by Dave, February 27, 2009
It took me a bit of googling to find your solution to why my textarea values were not being passed. Perhaps now you can help me figure out something else. I'm trying to use NicEdit and securimage for captcha live user confirmation. The captcha validation is handled via AJAX. Everything works until I execute NicEdit's saveContent() method, in which case the AJAX routine appears to get disabled. Ideas?
report abuse
vote down
vote up
Votes: +0
Captcha
written by netshine, February 28, 2009
Sorry, no idea - it's not the kind of thing I could answer without at least seeing the code (not that I have time to resolve other people's problems, unfortunately). You could try a different captcha script though, as there are quite a few available - one of them must be compatible with nicEdit!
report abuse
vote down
vote up
Votes: +0
Where to put
written by Elery, March 09, 2009
This is a really nice fix but where exactly do i need to put this code... could you give me an example? please?
report abuse
vote down
vote up
Votes: +0
Where to put it
written by netshine, March 09, 2009
You put it just before your javascript form submission. For example if you had a button containing an onclick event like this:

onclick="document.myForm.submit();"

It would now look like this:

onclick="above_code_goes_here;document.myForm.submit();"

Note: I had to use 'above_code_goes_here' instead of reproducing the code because this commenting system will not allow less than or greater than characters.

That is assuming you have a nicEdit editor named editor1.
report abuse
vote down
vote up
Votes: +0
SaveContent
written by Chimera, November 20, 2009
I just wanted to share with you my own way to use javascript to submit a textarea properly:

onclick="nicEditors.findEditor('textarea_id').saveContent();document.myForm.submit();"

This is faster if you have only one instance/textarea.
report abuse
vote down
vote up
Votes: +4
Lucky me
written by Nadun, December 16, 2009
I was lucky to find your post, smilies/smiley.gif thanks you solved my problem smilies/smiley.gif
report abuse
vote down
vote up
Votes: +0
Thanks
written by Sean, March 05, 2010
Thanks man! I appreciate your solution was a real savor!
report abuse
vote down
vote up
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security code
Write the displayed characters


busy
Joomla! Tutorial
Take a look at our free tutorial for Joomla! Administrator...


Login Form





Lost Password?

© 2004-2010 Netshine Software Limited   Terms and Conditions