The listelements are hidden by default, I’d like to select a number from the ‘postnr’ number input then show the listelements. I’m not sure what event I should be using with DOM. I figured I have to somehow loop through the list then run it same number of times as the input. Any advice?
JavaScript
x
6
1
let blogNr = document.getElementById("postnr");
2
var scene = document.getElementById("blogposts");
3
4
blogNr.oncontextmenu {
5
console.log("you changed the form");
6
}
JavaScript
1
5
1
ul {
2
font-size : 20px;
3
padding : 10%;
4
display : none;
5
}
JavaScript
1
7
1
<input type="number" id="postnr" name="postnr" min="0" max="5">
2
3
<ul id="blogposts">
4
<li>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Beatae fugit ut quod reiciendis asperiores animi vitae incidunt aspernatur ullam autem.</li>
5
<li>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Beatae fugit ut quod reiciendis asperiores animi vitae incidunt aspernatur ullam autem.</li>
6
<li>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Beatae fugit ut quod reiciendis asperiores animi vitae incidunt aspernatur ullam autem.</li>
7
</ul>
Advertisement
Answer
For your reference, here is a list of events – https://www.w3schools.com/tags/ref_eventattributes.asp
Not sure what you are asking exactly, so here are some possible solutions:
- You want the
<ul>
element to show when<input>
has a valid value:
JavaScript
1
9
1
let blogNr = document.getElementById("postnr");
2
let scene = document.getElementById("blogposts");
3
4
blogNr.addEventListener(`change`, (event) => {
5
const inputHasValue = !isNaN(event.target.value) && !!event.target.value;
6
const useDisplayStyle = inputHasValue ? `block` : `none`;
7
scene.style.display = useDisplayStyle;
8
});
9
- You want the
<li>
element whose index matches the number input’s value (for ex. if number is 1, show the first<li>
) to show:
in that case, it would not make much sense or there is little benefit in using <ul>
this way.