I will add a click add event listener, but what I am trying to achieve is this:
when the button is clicked I want to search the article HTML tang which doesnât have a "noneâ class. In a later situation, the article will be the first among its siblings, which will not have a class any, but it is dynamic later in the script. which JS API should I follow to get that sibling node where we do not have a none class?
So when it comes to âill-definedâ (read as: not a specific motion; not âmove to nextâ, or âmove 3 siblingsâ, but âmove somewhereâ) tree traversal, most of your tools are going to be looking up or down, rather than sideways.
So itâs easier in these cases to go up (parentNode), and then search downward.
Hi there @m_hutley
I completely didnât understand what you were saying, but I came with a version that is working â
const buttonClick = document.querySelector(".eventclick");
buttonClick.addEventListener('click', event => {
var traverse = buttonClick.parentElement.querySelector("article:not(.none)");
console.log(traverse);
traverse.classList.add("none");
var nSibling = traverse.nextElementSibling.classList.remove("none");
});
If you can spend some minutes to give feedback and is there any alternative precise method, which will exert less gravity on DOM manipulation in this case.
But there is adead ened in the script both ways, when the script reaches the bottom of the list of last sibling, next button should be dismissed or atleast deactivated, and same goes for top most sibling, when the traversal reaches the top most sibling previoous should be disabled or dismissed, but I do not have any logical clarity how to move forward from this point.
Hi @codeispoetry, I donât think such DOM manipulations would be an issue but you donât need to traverse the siblings on each click â just query for the slides initially and store the currently active index.
You can then set the button disabled states depending on that index like so:
const nextClick = document.querySelector('.next')
const previousClick = document.querySelector('.previous')
const slides = document.querySelectorAll('.pic-text-slide')
let active = 0
function switchSlide (next) {
active = Math.max(0, Math.min(slides.length - 1, next))
nextClick.disabled = active === slides.length - 1
previousClick.disabled = active === 0
slides.forEach((slide, index) => {
slide.classList.toggle('none', index !== active)
})
}
nextClick.addEventListener('click', () => switchSlide(active + 1))
previousClick.addEventListener('click', () => switchSlide(active - 1))
Alternatively, make the slides loop infinitely:
function switchSlide (next) {
active = (next + slides.length) % slides.length
slides.forEach((slide, index) => {
slide.classList.toggle('none', index !== active)
})
}
Hi there @m3g4p0p
Thank you so much for your input.
Is there any quantitative reason such as browser memory consumption, overload on some browser performance etc?
class none, in some real example, will be replaced by class with name = holidayclass
This holidayclass will be used on many inside elements such as .holidayclass .picture img {css here such as translateX} , in those articles or div tag for animation and image slides later so the class was needed. I will take a closer look at your code.
No, such DOM queries (especially if only on click) certainly wouldnât be an issue⌠itâs just not necessary either, so I was merely sketching out another structural approach. :-)