-5

I have a div in which it contains a body element as follows:

<div id = "TextBox1">
<iframe id = "TextBox1_1">
#document
<html>
<head></head>
<body></body>
</html>
</iframe>
</div>

Now I want to set a value to the body and tried

$('#TextBox1').attr('body', "SomeValue");

But it is not showing any value. How can this be done

Dheeraj Patnaik
  • 345
  • 1
  • 5
  • 18
  • what is `FreeTextBox1_designEditor`?? – Sushil Aug 31 '15 at 16:28
  • I am making an assumption here but I believe you are going to be making invalid HTML due to have multiple body tags. Please see http://stackoverflow.com/questions/2035462/multiple-htmlbody-html-body-in-same-file . – IfTrue Aug 31 '15 at 16:30
  • https://www.codecademy.com/tracks/web Here you go! – Steyn Aug 31 '15 at 16:34
  • Assume that is the only body tag and tell me how can I set the value @Saar – Dheeraj Patnaik Aug 31 '15 at 16:36
  • Assuming that is the only body tag, the div should be inside of it and not the other way around. – IfTrue Aug 31 '15 at 16:48
  • I have got a `dll` and it contains the structure as I mentioned in my question @IfTrue – Dheeraj Patnaik Aug 31 '15 at 16:58
  • I do not see anywhere in your question where you mention a dll. Additionally as others have said you have two elements with the same ID. Is the iframe going to be of the same domain as the main page? If not you will not be able to edit elements inside of it. Iframe is more of a "browser" than just another element. Please see: http://stackoverflow.com/questions/1451208/access-iframe-elements-in-javascript . – IfTrue Aug 31 '15 at 17:14

2 Answers2

-1

I'm sorry but two elements can't have the same ID, and you've not close the ID string of the div.

So if I rewrite a little bit the code, then you should have :

<div id="TextBox1">
    <iframe id="iframe_TextBox1">
        <html>
            <head></head>
            <body></body>
        </html>
    </iframe>
</div>

And then you do:

$("#iframe_TextBox1 > body").html("SomeValue");
-1

If you have included the JQuery simply put an ID in to your Body tag

<body id="MyID" ></body>

$("#MyID").html("<div>The stuff I want to add to the body</div>");

or without an ID you can just put:

$("body").html("<div>The stuff I want to add to the body</div>");

IfTrue is correct that you can't access the body with the code above from outside the Iframe, however the page that is inside the frame could contain the code to change the body.

As for accessing it outside the iframe you could use the following:

         var ifrBody = $("#MyFrame").contents().find("body");
         $(ifrBody).html("<div>My Stuff goes here</div>");

but only if you are within the same domain... you will get access denied if you try to do this across different domains. It is a security feature to prevent cross site scripting.

jon
  • 7
  • 3