Convert jQuery to DOM javascript

Here the jQuery code I want to convert to Javascript.

$(document).ready(function () {
    /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) || $("input").focus()
});

What are you struggling with?

Here’s how to replace the $(document).ready function: DOMContentLoaded event

Here’s how you replace the $: document.querySelector

Like this?

window.addEventListener('DOMContentLoaded', (event) => {
    /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) || document.getElementById("input").focus()
});

I can’t say, have you tested it, does it work?

getElementById, as the name says, selects an element by its id. If your input looks like this, it’ll work:

<input id="input">

Otherwise, you can use document.querySelector('input').

1 Like

Thanks for your help. It does work. :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.