An effective approach is to assign a class that can hide both the current element and the following one simultaneously.
If you apply the "hideElement1" class to the parent, it will conceal element1 and element2, as well as any subsequent elements.
This method is suitable for a limited number of elements; otherwise, employing JavaScript would be more practical.
<div class="parent">
<div class="element1">element1</div>
<div class="element2">element2</div>
<div class="element3">element3</div>
<div class="element4">element4</div>
</div>
<label for="tbx">insert number</label>
<input type="number" id="tbx" name="tbx" value="" />
<input type="button" id="btn" name="btn" value="click to hide" />
<style>
.parent.hideElement1 .element1, .parent.hideElement1 .element2 {
display: none;
}
.parent.hideElement2 .element2, .parent.hideElement2 .element3 {
display: none;
}
.parent.hideElement3 .element3, .parent.hideElement3 .element4 {
display: none;
}
</style>
<script>
var btn = document.getElementById('btn');
var tbx = document.getElementById('tbx');
btn.addEventListener('click', function () {
hideElementbyIndex(tbx.value);
});
function hideElementbyIndex(elementIndex) {
var elementClassNamePrefix = 'element';
var elementsToHide = document.getElementsByClassName(elementClassNamePrefix + elementIndex);
hideAll(elementsToHide);
elementIndex++;
var nextElementsToHide = document.getElementsByClassName(elementClassNamePrefix + elementIndex);
hideAll(nextElementsToHide);
}
function hideAll(elements) {
for (var i = 0; i < elements.length; i++) {
elements[0].style.display = 'none';
}
}
</script>