Reputation: 27
When I type the code:
document.queryselector('.classname').
.style and other common suggestions do not appear in the suggested autocomplete menu when I begin to type after the parentheses.
But when I type
document.queryselector('body').
It does?
Upvotes: 2
Views: 1376
Reputation: 2607
First of all, you should use querySelector
, not queryselector
.
Regarding your issue, there is a reason why VScode IntelliSense doesn't show style
attribute.
Let's look at this example:
const element = document.querySelector('.classname')
const element2 = document.querySelector('body')
element
has a type of Element. And element2
has type HTMLBodyElement which is subclass of HTMLElement.
Element
is the most general base class from which all element objects (i.e. objects that represent elements) in a Document
inherit. It only has methods and properties common to all kinds of elements. More specific classes inherit from Element
. And it doesn't have style
attribute.
The HTMLElement
interface represents any HTML element. It extends Element
and has style
attribute.
element2
return HTMLBodyElement
?VSCode has hardcoded typing when you selecting by exact tag name. You can see that in lib.dom.d.ts
You can add additional checking and VSCode will understand it:
const element = document.querySelector('.classname')
if (element instanceof HTMLElement) {
element.style.display = 'none'
}
Upvotes: 4