I have an element in my DOM where I have attached an ID. I want to call focus on that element after the Page Loads and set it to a css style (border: yellow) to highlight that it's currently focused. This is what I have:
//main.html
<myElement id= 'myEl'>
//main.js
window.setTimeout(function () {
document.getElementById('#myEl').focus();
}, 0);
When I refresh the page I receive this error:
Uncaught TypeError: Cannot read property 'focus' of null
That because javascript can't find any element with id='#myEl'
, remove the extra #
in :
document.getElementById('#myEl').focus();
_________________________^
Or use jquery selector instead :
$('#myEl').focus();
Component ID changes in runtime, so use a class rather than ID, will work, like
$(".ClassName").focus();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With