1


I want to focus on iframe every time I click on <button class="bold" >bold</button>
But It Only Works one time
Here is my code :
JavaScript :

function iFrameStart() {
    iframe.document.designMode="On";
}
document.addEventListener("DOMContentLoaded",iFrameStart,false);
$(document).ready(function () {
    $(".bold").click(function () {
       iframe.document.execCommand('bold',false,null);
       document.getElementById("iframe").focus();
    });
});

HTML :

<div class="container">
    <h1>Editor Demo</h1>
    <div class="button-wrapper">
        <button class="bold">B</button>
    </div>
    <label><textarea name="" id="" cols="30" rows="10"></textarea></label>
    <iframe id="iframe" name="iframe" ></iframe>

</div>
Shawn Vn
  • 297
  • 1
  • 14

1 Answers1

1

Maybe it is a solution for you.

$('#iframe').contents().find('html').focus();

And vanilla js

const frame = document.querySelector('#iframe').contentWindow;
frame.document.querySelector('body').focus();
Grzegorz T.
  • 3,903
  • 2
  • 11
  • 24