Sometimes, we want to fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript.
In this article, we’ll look at how to fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript.
How to fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript?
To fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript, we should make sure we call it with a valid selector string.
For instance, we write
const el = document.querySelector("[id='22']");
to call querySelector
with "[id='22']"
to get the element with ID 22 since valid IDs can’t start with numbers.
Conclusion
To fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript, we should make sure we call it with a valid selector string.