0

Is there a way to retrieve the content from iframe and store it in textarea using pure JavaScript?? I have retireve the content from iframe using following code:

function getIframeContent(){
 var frameObj = document.getElementById(frameId);
 var frameContent = frameObj.contentWindow.document.body.innerHTML;
 alert("frame content : "+frameContent);
 }

Now how to store it in the textarea??

john smith
  • 190
  • 2
  • 11
  • Take a look at: [How to change the Content of a – RodrigoDela Dec 10 '14 at 10:09
  • but can it store styled text content in textarea that i have taken from iframe?? – john smith Dec 10 '14 at 13:21

1 Answers1

0

You can display the content of your iFrame inside your TextArea like so

 document.getElementById('YourTextArea').innerHTML = frameContent;

or using the Value attribute:

document.getElementById('YourTextArea').value = frameContent;

I have done a quick demo for you using the first method.

This returns a string with any html tag in it. So for example if your iframe contains a list, the method returns an html list.

Here's the code:

<body>
    <div id="frameId">
      <ul>
        <li id="item1">Coffee</li>
        <li id="item2">Tea</li>
      </ul>
    </div>

    <p>Click the button get the HTML content of the iframe.</p>

    <textarea id="demo"></textarea><br/>

    <button onclick="myFunction()">Try it</button>

  <script>
    function myFunction() {
        var frameContent = document.getElementById("frameId").innerHTML;
        document.getElementById("demo").innerHTML = frameContent;
    }
  </script>
</body>
U r s u s
  • 6,680
  • 12
  • 50
  • 88
  • Is it the tone of my answer or its format you're complaining about?If it's the former, I'm not critising anyone; if it's the latter, I've seen a lot of answer like this all over SO. – U r s u s Dec 10 '14 at 10:46
  • 1
    Looks more like a suggestion indeed. And you are right, SO needs a little bit more cleaning, despite tremendous efforts provided by the community. – Jean Dec 10 '14 at 11:39
  • The use of "Why not trying" suggests it is not an answer, but a request for clarification. – Mark Rotteveel Dec 10 '14 at 11:51
  • but can it store styled text content in textarea that i have taken from iframe?? – john smith Dec 10 '14 at 13:23
  • @Dura thnx for your answer but i am not getting what i desire. as from your example when we clicked the try it button we get answer like :
    • Coffee
    • Tea
    but i was trying to get the styled text in textarea that we have in iframe but we are getting html structure of text so i was trying to find that solution . anyway thanks for your concern!!
    – john smith Dec 11 '14 at 02:44
  • @Dura Some one was flagged your answer as "its not an answer". I just reviewed your answer before your updates. Yes I agree there are many answers like this. But the **flagged** Q/A will be reviewed and appropriate comment will be added by `stackoverflow` based on our reviews. – RajeshKdev Dec 11 '14 at 03:54