0

here the inspectI have attach iframe to my site and I have try many time to put value to iframe text box. But not working, Is there any way to put value to iframe.

I have try with

var test = $('#text').contents();

 test.value="WELCOME";

Here My example.com/test/index.html

      <html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <div>
        <label>Filed1
        <label>
           <textarea id="text"></textarea>
     </div>
      </body>
       </html>

My iframe:

   <iframe id ="test" width="350" height="400" 
   src="www.example.com/test/index.html" frameborder="1"  ></iframe>
nur islam
  • 67
  • 8

2 Answers2

0

Have you tried val ?, it will work on only same domain link !

$('#test').contents().find('#text').val('WELCOME');

Please make sure you put the javascript AFTER the iframe or put this in a onload event called from the iframe

thanks :)

Murtaza Hussain
  • 3,851
  • 24
  • 30
0

For IFrame there is a property named contentWindow, as so the frame contains the whole html page inside ; and there is the document which you can access to the inner contents of the iframe.

// html:
// <iframe id="a"></iframe>

var f = document.getElementById("a");
var fd = f.contentWindow.document;

to clear the content or address the src :

// clear content 
element.src = "about:blank";

to write the content:

// write new content
var newHtml = '<html><body><textarea id="aa">Hello</textarea></body>'
f.contentWindow.document.open();
f.contentWindow.document.write(newHtml);
f.contentWindow.document.close();

and to access inner content elements:

fd.getElementById("aa").value = "orange";

// or in JQuery :    
$("#aa",fd).val("Hello again");
nullqube
  • 2,959
  • 19
  • 18