0

I have an iframe and a textarea and I want to add an Edit button, so the user can edit his/her articles (like in StackOverflow you can edit the questions). I want to put all the existing data of an article into a textarea and then equal the data with the iframe. Here is my existing code, when the user creates an article.

HTML:

<textarea style="display:none;" name="myTextArea" id="myTextArea" cols="100" rows="14"></textarea>
<iframe name="richTextField" id="richTextField"></iframe>
<button id="article_btn" onclick="saveArticle()">Save Article</button>

Javascript:

function saveArticle(){
    var theForm = _("writearticle");
    theForm.elements["myTextArea"].value = window.frames['richTextField'].document.body.innerHTML;
    theForm.submit();
}

So it there any way to get the existing article data from the database and put it in the textarea?

squancy
  • 565
  • 1
  • 7
  • 25
  • An iFrame is probably the wrong element to use in this case. Why not just show the content in a DIV? Do you have a specific need to use iFrame? Since we do not know what server-side tech you are using, we cannot help much with loading from database. If you were calling the data from a web service using AJAX, just dump the responseText into the value of your textarea document.getElementById('myTextArea').value = xhr.responseText – daddygames Aug 17 '17 at 14:17
  • If you are loading the article into the iFrame and want to copy that content over, you can view this article to see how to get the content from an iFrame https://stackoverflow.com/questions/926916/how-to-get-the-bodys-content-of-an-iframe-in-javascript – daddygames Aug 17 '17 at 14:21
  • @daddygames That `iframe` works as a text editor, that's why I'm using that. I want to copy the value of the `textarea` and equal with the `iframe` (see in JS), because this is the most easiest and maybe the only way to get the value of an iframe. But here I don't want to see plain `iframe`, I want to put the value of an article that the user has already posted. And now, the user can edit the article. – squancy Aug 17 '17 at 15:01
  • I use http://jqueryte.com/ – daddygames Aug 17 '17 at 18:45
  • This provides a WYSIWYG text editor that does what you are looking for, but does not use an iFrame. If you want to continue with your custom implementation, then you might need to inspect your code to ensure you are selecting the correct elements and data from the page. – daddygames Aug 17 '17 at 18:48
  • var textArea = document.getElementById('myTextArea'); var iFrame = document.getElementById('richTextField'); var iFrameBody; if ( iFrame.contentDocument ) { // FF iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0]; } else if ( iFrame.contentWindow ) { // IE iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0]; } textArea.value = iFrameBody – daddygames Aug 17 '17 at 18:52

0 Answers0