-1
I have this code:
let inicial = -0;
const numeros = [1,2,3,4,5,6,7,8,9,10];
function proximo (){
inicial++
document.getElementById("lugar").innerHTML=''
alert(inicial)
for(let i = inicial-1; i < inicial+2; i++){
const ul = document.createElement('UL');
const numerosUl = document.createTextNode(numeros[i]);
const lugarElement = document.getElementById("lugar");
ul.appendChild(numerosUl);
lugarElement.appendChild(ul);
}
}
function limpar(){
document.getElementById("lugar").innerHTML=''
}
<html>
<button onClick="proximo()"> Próximo </button>
<div id="lugar"> Teste </div>
<button onClick="limpar()"> Limpar </html>
</html>
It is appearing in 3 in 3 numbers, however, I want, every click it updates itself with the own numbers of the sequence array.
Example: If you click now, it will appear 1,2,3. I want that when you click the next time, appear: 4,5,6 and the next: 7,8,9 and at the last click, appear: 10. Not creating new elements in the body but updating it.
Could someone help me and if possible explain what was done? I just want this change, starting from the number that does not appear on the screen, that at first it is, 1 to 3, and starting from the 4 and go to 6 and start from the 7 and go to the 9 and when last click appear the 10.
What is the difference between this doubt and your doubt previous question? The answer there did not solve the problem?
– bfavaretto
@bfavaretto no, it 'ate' the number 7. N know why.
– Jota
Okay, I get it. But you could have commented on that in @dvd’s own reply to the other question, I’m sure it would review the code. When you mark the answer as accepted (green tick), as you did there, it looks like the problem is solved.
– bfavaretto
Yes, I hadn’t even noticed it was my friend who saw it, but it helped me with that code, I understood some things
– Jota
Another thing to watch out for, that only one of the answers below corrects: you are generating invalid HTML, you cannot insert a direct Node text into UL. You need a LI around the Node text, and the LI yes goes inside the UL.
– bfavaretto