Code not working

window.onload = function (){
let num = document.getElementById(‘num’)
let list = document.getElementById(‘list’)
let print = document.getElementById(‘print’)

print.onclick = function(){
let N = parseInt(num.value)
for(let i =1; i <= N; i++) {
list.innerHTML += ‘

  • ’ + i +’

  • }
    }
    }

    Hi @ravip413131,
    Using innerHTML to append html elements (e.g. el.innerHTML += "<a href='...'>link</a>" ) will result in the removal of any previously set event listeners. That is, after you append any HTML element that way you won’t be able to listen to the previously set event listeners.

    Therefore I have added the below code please have a look :

     window.onload = function () {
      let num = document.getElementById("num");
      let list = document.getElementById("list");
      let print = document.getElementById("print");
    
      print.onclick = function () {
        let N = parseInt(num.value);
        for (let i = 1; i <= N; i++) {
          list.innerHTML = "<li>" + i + "</li>";
        }
      };
    };
    

    For more precise information refer the provided Link.

    I hope I’ve cleared your doubt. I ask you to please rate your experience here
    Your feedback is very important. It helps us improve our platform and hence provide you
    the learning experience you deserve.

    On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
    the doubt.