Javascript for setting focus on a search input field in a popup

I am trying to set the focus on a search field in a popup window but can’t seem to get it to work. The code I am using is below.

$(document).on(‘knack-scene-render.scene_142’, function(event, scene) {
$(‘#view_277 input’).focus();
});

The intent is to place the cursor in the input box when the pop up window opens, but it is not working.

Any help would be greatly appreciated.

If you’re certain that both the selectors for the scene ID and view ID are correct, you can add a delay before executing the focus() function. It is recommended to use the input’s ID instead of the ‘view_id input’.

Here’s an example code snippet:

$(document).on('knack-scene-render.scene_id', function(event, scene) {
  setTimeout(function() {
    $('#field_id').focus();
  }, 1000);
});

Thanks Josh. The input field is a search box in a popup window. I can’t determine what the id is. I also tried using a timer with the code but it still didn’t work. Thanks for taking a look.