views:

492

answers:

3

I am using the openwysiwyg editor in my webpage. I want to clear the contents of it. I have used

$('#report').val('');

but that doesn't clear it.

The editor creates an iframe and updates the contents there, syncing as it goes.

How would I go about clearing it?

A: 

I believe this works

$('#report').text('');
lyrae
+2  A: 

You probably need to supply a bit more information - the html itself would be very useful, but I'm going to assume that report is the id of the textarea you need cleared.

If it's a normal textarea, your code should really work.

If (as Paulo mentions in the comments) it's being modified by an openwysiwyg editor, it's probably being turned into an iFrame with it's own HTML page in it. It's a lot more difficult to manipulate the iFrame.

Looks like that's the case.


Have a look at this example to see if it helps you reference the iFrame itself: http://www.bennadel.com/index.cfm?dax=blog:1592.view


This is a hacked excerpt of the example.html that comes with openwysiwyg:

<script type="text/javascript">
 // Use it to attach the editor to all textareas with full featured setup
 //WYSIWYG.attach('all', full);

 // Use it to attach the editor directly to a defined textarea
 WYSIWYG.attach('textarea1'); // default setup
 WYSIWYG.attach('textarea2', full); // full featured setup
 WYSIWYG.attach('textarea3', small); // small setup

 // Use it to display an iframes instead of a textareas
 //WYSIWYG.display('all', full);  

 function getIFrameDocument( id )
 {
  var iframe = document.getElementById(id);

  if (iframe.contentDocument) {
   // For NS6
   return iframe.contentDocument; 
  } else if (iframe.contentWindow) {
   // For IE5.5 and IE6
   return iframe.contentWindow.document;
  } else if (iframe.document) {
   // For IE5
   return iframe.document;
  } else {
   return null;
  }
 }

 function clearcontents()
 {
  getIFrameDocument('wysiwygtextarea1').body.innerHTML = '';
 }
</script>

Then somewhere in the page, I've got a clear button (actually div):

<div style="width:120px;height:20px;background:#ff0000;text-align:center;display:block;" onclick="clearcontents();">Clear!</div>


Note that the id of your textarea is prefixed with wysiwyg. That's the name of the iFrame.

I've tested this in Firefox but nothing else at the moment. The code for getting the iFrame I found on the Net somewhere, so hopefully it works for other browsers :)

Damovisa
Hi Damovisa, it is definately using an iframe. I just learned this myself in a prior post today. This is aparently not a "normal" editor.
@nutjob - yeah, I encountered the same error myself not long ago.One suggestion could be disabling the wysiwyg (if you can), clearing the original textarea, and setting the wysiwyg again?
Damovisa
I've updated my answer with a link to an example that might help. You may be able to reference the iFrame itself and clear it that way...
Damovisa
Hey Damovisa, that sounds like what I should do, unfortunately, I haven't the slightest idea how to do that. Paolo was saying that it is involved to so. How did you resolve yours?
I was able to achieve what I wanted a totally different way - I didn't need to change the text really.I'm not convinced it is actually that difficult - you should be able to reference the iFrame by using the window.frames collection...
Damovisa
That sounds like great news. I have to tell you though... I have absolutely NO experience with javascript what so ever. If you think it would be easy enough for a total noob to do, I'm willing to give it a whirl. That is if you're up to the task. :) If you have a very specific howto or something, it would be greatly appreciated.
I checked out the link you proviced and it certainly is a small piece of code but I have no idea what to do with it. :D
Ok, I'll update my answer :)
Damovisa
Thank you Damovisa!
Done - refresh though, I made a mistake first time round
Damovisa
Hmm, ok, that won't work for openwysiwyg - let me look again...
Damovisa
Thanks Damovisa. Paolo provided a piece of code too. He seems to really know this editor inside and out. Have you ever used it? Thanks again for all the help.
Nope - never used that one sorry! I've used the jWYSIWYG plugin from here: http://plugins.jquery.com/project/jWYSIWYG. Maybe try that if you have no luck with this one?
Damovisa
Damovisa, I have been trying to get this code to work but I get nothing. I even tried it in FF and still nothing. Am I doing something wrong? I put the function at the top of the JQ code, outside of document.ready() and call the function inside the JQ success(). I even tried to put the function inside of document.ready but it still won't work.
Ok, let me have a look at it again :)
Damovisa
Sorry for being such a noob, Damovisa..
Ok, I downloaded their demo and modified it, and apart from not being able to get jQuery to play nice, it seemed to work. I'll update my answer with the complete code I used.
Damovisa
Thank you Damovisa!! I hope I can get this to work, especially after all the help that has been given to me.
@nutjob - no problem :)I've tested in Firefox and IE. The wysiwig thing claims it doesn't run in Safari or Chrome though. Watch out for the id property - make sure it matches the iFrame id not the textarea id (should start with "wysiwyg")
Damovisa
Damovisa, That clears the textarea!! I can use the button and it wipes everything out. :D .... It even works in Opera. I already know about openwysiwyg not working in chrome. Thanks for the heads up. Is there a way to get the iframe to clear without the button? The idea is that after the user saves the data and it was successful, the iframe should clear. If the user has to press a clear button, it kinda defeats the purpose I guess. Its just that users are not that smart and because JQ doesn't refresh on submit, the user will keep pressing the save button.
nutjob: Just save his functions somewhere in your code and call clearcontents(); in the success callback of the ajax request.
Paolo Bergantino
That was exactly what I was thinking.. Ok.. so within the success part of JQ, I should be able to just call clearcontents(); and have it clear..
@nutjob - yes, what Paulo said :) I'm calling clearcontents in response to a click. If you call it in the success callback of the ajax request it should work fine.
Damovisa
You know. I think I have some errors in my jq code because for some reason even though the form submission was successful, I can't see the alert() I put in the success function. I just added the alert because I still couldn't get the clearContents to work. I should be able to figure this one out though. I'm going to play with this and see what may be stopping it.
I have this before my success function: var form = $('#rpt'); rpt is the form name but for some reason I can't alert anything inside of the success function. Am I correct that rpt is my form name as well as the var?
Is there a way for me to see in firebug what exactly the form name is that is being posted?
you should be able to see it in your html (in firebug)
Damovisa
Thanks Damovisa. I know that when I get this part fixed that your code will work. Please let me ask you another question if you don't mind. I have the following in my jq code: var form = $('#rpt'); var data = form.serialize(); var type = form.attr('method'); var url = form.attr('action');If I do alert(form), what should I see? All I get is [object Object]
yeah - the alert won't tell you much about your object to be truthful - it's not intelligent enough to break it apart for you. A better way to discover stuff is through firebug by enabling the console for that site. You can type javascript directly in and it will give you the results.
Damovisa
Also, I'm about to leave the computer for the day. I probably won't be able to respond for a while. Hope this has all helped!
Damovisa
Actually it has, Thanks so much Damovisa. You and Paolo went above and beyond. I'll post back when I figure this out. Thanks again!
+1  A: 

This works, but is butt ugly:

var frame = WYSIWYG.getEditor('--ENTER EDITOR NAME HERE--');
var doc = frame.contentWindow.document;
var $body = $('html',doc);
$body.html('');

Replace --ENTER EDITOR NAME HERE-- by whatever you pass to the editor when you call attach.

Paolo Bergantino
LOLOL.... What are you talking about? It looks great from my perspective. :D ..... I'll try this and come back in a few.
Paolo. Here is what I have for the attach: WYSIWYG.attach('report', rpt); ... Am I correct in replacing the editor name with 'report'?
Hi Paulo - that should work for IE - not sure about Firefox - it seems to use a different way of referencing iFrames...
Damovisa
I'm using Opera and it isn't working. I currently using Paolo's code but nothing is happening.. Its possible that I don't have the correct editor name though.
report should be fine as the editor name. @Damovisa: It works for me on Firefox. Not sure about Opera...
Paolo Bergantino
nutjob: Try Damovisa's updated code, which seems to take a lot of the browser differences into account. This editor is borderline retarded. Is it too late to switch? :)
Paolo Bergantino
hahaha.. Actually, no it isn't too late. Even if it was too late, if this is ANY indication as to what I have to go through to get JQ to play nice with it, I think I'd rather throw it out. Here's what makes me hesitate.. I looked at fckeditor but I don't care for how the interface looks (it looks cheap.. kinda like my ex) and it weighs in at about 1.5 megs which is heavy (again, like my ex :)). openwysiwyg is only 200KB and loads really fast. The only thing that keeps me tied to this editor is the speed. fck couldn't even compare. Naturally because it loads more stuff. Convince me please.
@nutjob: Have a look at http://plugins.jquery.com/project/jWYSIWYG. It's the one I'm using. It gives you a lot of options for your UI but I don't think it's quite as comprehensive as the one you're currently using.
Damovisa
Actually, I like it, alot. It looks sharp. I'd need to add a submit button though, right?
huh.. There is no documentation with jWYSIWYG.
nutjob: What about TinyMCE?
Paolo Bergantino
Actually, I'm totally cool with jWYSIWYG. I really like it. I just wish there were some docs that went with it. Again, it goes back to what I was telling you about JQ developers expecting everyone to know how to implement it. So how does a noob go bout using this editor? Anyhow, given TinyMCE or FCK, I'd probably take FCK.
Yeah, there isn't really much in the way of doco... I've been doing this a while so I just read the source :) Maybe FCK or TinyMCE are the way to go.
Damovisa
I'm over here kind of laughing because I can see why there is no documentation. I thouht that the editor was totally configurable but in reality, it is like one line to use it. :) Yeah, I looked at the code but I dont understand it. I really like jWYSIWYG though because it is totally lightweight. The users don't need file uploads or anything like that. If this openwysiwyg editor doesn't work afterall, I will probably just use the jq plugin