How to keep focus on an input in an iframe?

Member

by lottie , in category: HTML & CSS , 2 months ago

How to keep focus on an input in an iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 2 months ago

@lottie 

To keep focus on an input in an iframe, you can use JavaScript to programmatically set the focus on the input element within the iframe. Here is an example code snippet that demonstrates how to achieve this:

1
2
3
4
5
6
7
8
// Get the iframe element
var iframe = document.getElementById('iframe-id');

// Get the input element inside the iframe
var input = iframe.contentDocument.getElementById('input-id');

// Set the focus on the input element
input.focus();


Make sure to replace 'iframe-id' and 'input-id' with the appropriate IDs of the iframe and input elements in your HTML. This code should be executed after the iframe has loaded, either by placing it at the end of the HTML body or by listening for the iframe's load event before running the code.