0

DOM

how can i select an element's first line in javascript ?

7th Aug 2021, 4:22 AM
Noname
Noname - avatar
7 Réponses
+ 2
const selectFirstElement = document.querySelector('.element:first-child');
7th Aug 2021, 5:06 AM
Calviղ
Calviղ - avatar
+ 2
firstElementChild and children are read only properties, cannot use for update element nodes.
7th Aug 2021, 8:42 AM
Calviղ
Calviղ - avatar
+ 1
there aren't a selector for first line, only for first letter to if you are looking for first child element you can try with const container = document.getElementById("myelement"); const first = container.firstElementChild; or const container = document.getElementById("myelement"); const first = container.children[0];
7th Aug 2021, 6:19 AM
David Ordás
David Ordás - avatar
+ 1
Calviղ querySelector is not compatible with older browsers. The requisites is only read https://caniuse.com/?search=querySelector
7th Aug 2021, 1:13 PM
David Ordás
David Ordás - avatar
+ 1
David Ordás how old the browser It's not compatible with? IE8 2008 browser perhaps, does it really matter?
7th Aug 2021, 2:24 PM
Calviղ
Calviղ - avatar
+ 1
Calviղ question to yourself what is the way to maximize posibilities and the answer to it is got of course always can be patched using polyfills to this residual browsers
7th Aug 2021, 2:39 PM
David Ordás
David Ordás - avatar
+ 1
David Ordás Most web developers would ignore support of very old browsers like ie8, which literally has no user using it now. Of course you could add polyfills to support it, if your project really need to support over 10 years old browsers, but I believe it's rare case, you only add the unnecessary code there, even though polyfills would not affect new browsers performance too much.
7th Aug 2021, 4:04 PM
Calviղ
Calviղ - avatar