I am trying to add the CSS(width:100%) using jquery into an iframe. But don't know where I am doing wrong. I tried the below mentioned code to add the CSS in an iframe using jquery. HTML File

     <div class="col-md-12">

       <div class="page-content ask-question" style="margin-top: 5px;">

           <div id="preview-container" style="height: 342px;">

           </div>

       </div>
   </div>

Jquery code

$('.ace_text-input').keyup(function(e) {

delay(function(){
    var html1 = buildSource(html, js, css);
    var iframe = document.createElement('iframe');

    iframe.src = 'about:blank';
    iframe.frameBorder="0";
      iframe.height = 496;
      iframe.css='width:100%;';
    iframe.className = 'preview-iframe';

    $('.preview-iframe').remove();
    $('div#preview-container').append(iframe);

    iframe.contentWindow.document.open('text/html', 'replace');
    iframe.contentWindow.document.write(html1);
    iframe.contentWindow.document.close();
},1000);
});

2 Answers

0
Community On Best Solutions

Instead of

iframe.css='width:100%;';

I Write this

iframe.setAttribute( "Style", "width : 100%;");
-1
gcurtis On

You can apply width on the element using Javascript Review the below code snippet. Thanks

var iframe = document.createElement('iframe').style.width = "100%";