This function finds and returns the first DOM element that matches the given CSS selecotr. Where no matches are found, NULL
is returned.
This works like Document.querySelector()
/ Element.querySelector()
but internally polyfills the CSS :is()
, (:matches()
, :any()
) pseudo-class function.
import select from '@web-native-js/play-ui/src/dom/select.js';
select(selector[, context = null]);
selector
-String
: A valid CSS selector.context
-HTMLElement
: An optional node under which to run the query. This would be equivalent tocontext.querySelector(selector)
.
HTMLElement
- The matched element.NULL
- if no matches were found.
The example below demonstrates this function with the CSS :is()
pseudo-class function.
<body>
<header>
<div>DIV1</div>
</header>
<main>
<div>DIV2</div>
</main>
</body>
let div = select(':is(header, main) > div');
console.log(div.innerText); // DIV1