Uncaught SyntaxError: Failed to execute 'querySelector' on 'Document'
You are allowed to use IDs that start with a digit in your HTML5 documents:
The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.
There are no other restrictions on what form an ID can take; in particular, IDs can consist of just digits, start with a digit, start with an underscore, consist of just punctuation, etc.
But querySelector
method uses CSS3 selectors for querying the DOM and CSS3 doesn't support ID selectors that start with a digit:
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.
Use a value like b22
for the ID attribute and your code will work.
Since you want to select an element by ID you can also use .getElementById
method:
document.getElementById('22')
Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '' is not a valid selector
Selecting elements with a particular ID using the #
syntax doesn't allow for an unescaped $
s inside the ID. You could bypass this by selecting the parent element via [id="<some id>"]
instead:
console.log( document.querySelector('[id="foo$bar"] .item'));
<div id="foo$bar"> <div class="item"> item </div></div>
Failed to execute query selector on document, id is not a valid selector
You might find jQuery easier, and it's automatically cross-browser (and faster to type!) Since it's tagged on your question, here is the jQuery solution:
Edit: The tag jQuery
was removed from the original question on May 25 '19 at 21:10 (3 years after question was asked and answered), by user John
, with the inexplicable editor comment "remove spam tags".
jsFiddle Demo
$('[id^=edit_]').click(function(){
var id = this.id.split('_')[1];
$('#'+id).addClass('hidden');
$('#edit_'+id).addClass('hidden');
$('#save_'+id).removeClass('hidden');
$('#editable_'+id).removeClass('hidden');
});
$('[id^=save_]').click(function(){
var id = this.id.split('_')[1];
$('#'+id).removeClass('hidden');
$('#edit_'+id).removeClass('hidden');
$('#save_'+id).addClass('hidden');
$('#editable_'+id).addClass('hidden');
});
Note that I switched around the id_number and the idName_ prefix. This makes it much easier to target those elements using the starts with
selector: id^=
Failed to execute 'querySelector' on 'Document': 'button[data-id="purple-button" disabled]' is not a valid selector
You should use brackets to separate the attributes.
document.querySelector('button[data-id="purple-button"][disabled]')
Per this Stack Overflow answer
SyntaxError: Failed to execute 'querySelectorAll' on 'Document': '#0.resizer' is not a valid selector
It is very hard to tell what could be the issue but as i can assume currently on the basis of your selector. The generated markup should look like:
<div id="0" class="resizer">...</div>
But if it is like:
<div id="0.resizer">...</div>
Then you should escape the .
as it has special meaning in css. It denotes class selector. So you should escape it like:
document.querySelectorAll("#" + id + "\.resizer")
//document.querySelectorAll(`#${id}\.resizer`) // <---es6 template variable
Why is there an error saying Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#1' is not a valid selector
The event listener requires the href
in the navigation link to be a valid selector for the element to scroll to. So change
<a class="nav__link nav__link--btn btn--show-modal" href="#">Open account</a>
to
<a class="nav__link nav__link--btn btn--show-modal" href="#someid">Open account</a>
and replace someid
with the ID of the element that contains the account opening form that you want to scroll to.
Why is there an error that occurs randomly saying Uncaught DOMException: Failed to execute 'querySelector' on 'Element': '' is not a valid selector
document.querySelector is not working with "=" symbol inside the id
So what is wrong with the
=
symbol in thisdocument.querySelector("#canvas-=")
As mentioned in the querySelector
docs:
To match against an ID or selectors that do not follow standard CSS syntax (by using a colon or space inappropriately, for example), you must escape the character with a backslash ("
\
"). As the backslash is also an escape character in JavaScript, if you are entering a literal string, you must escape it twice (once for the JavaScript string, and another time forquerySelector()
):
Working Demo:
const elem = document.querySelector("#canvas-\\=");console.log(elem.innerHTML);
<h1>This is a Heading</h1><p id="canvas-=">This is a paragraph.</p>
Related Topics
How to Wait for Element to Disappear in Cypress
How to Export Json Data to Excel File Using JavaScript
Angular Load Async Data Before Component Initialization
How to Count the Entire Number of Rows in a Datatable
Download Image as File in Typescript
Best Way to Show a Loading Spinner/Gif While My React Component Is Fetching Via Ajax
How to Use Zindex in React-Native
Regular Expression Which Allow Only Characters or Space
Why Flatlist Is Not Updating Dynamically in React Native
How to Access the Value of a Promise
Avoid a Link to Reload the Page in JavaScript
Youtube API - Failed to Execute 'Postmessage' on 'Domwindow'
How to Only Remove the "Watch Later" and "Share" Buttons from Youtube Iframe Embed Player