querySelector: what is the return type?
document.queryselector returns null
queryselectorall multiple classes
According to the MDN documentation,
querySelector returns an
Element. However, the return value is generally richer than that.
For example if I select an
img element, I really get a
HTMLImageElement which, according to the documentation, is part of this hierarchy:
EventTarget ← Node ← Element ← HTMLElement ← HTMLImageElement
My understanding of inheritance is that objects have access to properties further down the chain, but not further up.
Does this imply that
querySelector doesn’t actually return an
Element, but rather one of a number of objects which extend
Does this imply that querySelector doesn’t actually return an Element, but rather one of a number of objects which extend Element?
Any object which is a subclass of Element is still an Element. It is just something more specific as well.
What is the return type of document.querySelectorAll, Return Value: A NodeList object, representing the first element that matches the specified CSS selector(s). If no matches are found, null is returned. Throws According to the MDN documentation, querySelector returns an Element. However, the return value is generally richer than that. My understanding of inheritance is that objects have access to properties further down the chain, but not further up. Does this imply that querySelector doesn’t actually return an Element,
When a document indicates that a specific interface (
Element) is returned, it logically means that everything that implements that interface (
HTMLImageElement) is also counted, otherwise they'd have to list all the subclasses in particular which would've been redundant.
Document.querySelectorAll(), Return Value: A NodeList object, representing all elements in the document that matches the specified CSS selector(s). The NodeList is a static collection, HRESULT retVal = object.querySelector(v, pel); Parameters. v [in] Type: BSTR. The selector string. pel [out, retval] Type: [IHTMLElement](aa752279(v=vs.85).md) A DOM element node, or NULL if the search cannot find an element that matches the selector string. Return value. Type: HRESULT. If this method succeeds, it returns S_OK.
HTMLImageElement actually implements Element, instead of extending it, and retains all properties of
HTMLElement, which implements all properties of
instanceof checks on the objects returned by
querySelector is truthy for
Element, except when it's null. So, for querySelector it doesn't matter that the prototype of the returned element is of type
HTMLImageElement, as long as the element is implements all properties of
Element and provides a truthy check for
The element returned by querySelector is guaranteed to have all the properties of
Element or would be null, if you want to use other specialized properties, you'd have to do your own
document.querySelector, Search Terms querySelector, return, type, selector Suggestion This issue closely follows #8114, which applies only to type selectors Syntax of querySelector. Below is the syntax of querySelector: querySelector(CSS selectors) It returns the first element that matches the specified selectors. To return all the elements which match then we use the querySelectorAll() method. The CSS selectors which we pass should be of string type. It is mandatory to pass the CSS selectors.
querySelectorAll(), The QuerySelector method returns a Node rather than an Element. The issue is the return type for the method defined on the following line: The querySelector() method in HTML is used to return the first element that matches a specified CSS selector(s) in the document. Note: The querySelector() method only returns first element that match the specified selectors.
querySelector return type could be more specific for compound , The querySelector() method in HTML is used to return the first element that selectors are used to select HTML elements based on their id, classes, types, etc. The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned. Note: The matching is done using depth-first pre-order traversal of the document's nodes starting with the first element in
Element.QuerySelector returns a Node rather than an Element , The Document method querySelector() returns the first element within The value that's set to innerHTML should come from trusted sources, querySelector Summary. Returns the first element that matches the provided selector. Method of dom/Element dom/Element. Syntax var element = element.querySelector(/* see parameter list */); Parameters selectors Data-type String A selector, or multiple selectors (separated by commas). Return Value. Returns an object of type DOM NodeDOM Node
- Can I understand the use-case ? Could this be an XYProblem? xyproblem.info
- Congratulation, you've discovered something called polymorphism.
- It returns a
HTMLImageElement, or whatever querySelector finds, but the nice thing about a dynamic language, the return type can also be dynamic, static languages would require you to type-cast here,.