Javascript To Use Prevall Like Jquery?
How to achieve this in JavaScript? function prevAll (element) { // some code to take all siblings before element? return elements; };
Solution 1:
Use the previousSibling
property, and loop until it's null.
Solution 2:
You could use a generator, if the situation called for it:
const sibGen = function* (element) {
let sibling = element.previousElementSibling;
while(sibling !== null) {
sibling = sibling.previousElementSibling;
yield sibling
}
}
And then call it as:
const siblingGenerator = sibGen(myElement);
// filter(Boolean) removes the null elementconst allSiblings = [...siblingGenerator].filter(Boolean);
Post a Comment for "Javascript To Use Prevall Like Jquery?"