During a function call, the result is displayed on a new page (the address is the same, but the page is empty, and there is only a result on it).
What to do? How to display the text?

function someFunc() { var min = document.getElementById("min").value; var max = document.getElementById("max").value; var rand = min - 0.5 + Math.random() * (max - min + 1); rand = Math.round(rand); document.write(rand); } 

The result should be displayed between the tags.

 <span id="rnd-num"></span> 

    1 answer 1

    According to the MDN documentation , calling document.write() in an already loaded document will automatically call document.open() .
    Conclusion: document.write should be used only where it is really necessary, and with an understanding of how it works.

    Outputting the result to #rnd-nam using item selection by ID and replacing its text content ( textContent ):

     var button = document.getElementById("button"); var minElement = document.getElementById("min"); var maxElement = document.getElementById("max"); var result = document.getElementById("rnd-num"); function someFunc() { var min = minElement.value; var max = maxElement.value; var rand = min - 0.5 + Math.random() * (max - min + 1); rand = Math.round(rand); result.textContent = rand; }; button.addEventListener("click", someFunc); 
     <input id="min" value="1" /><br/> <input id="max" value="100" /><br/> <input id="button" type="button" value="Generate" /><br/> <span id="rnd-num"></span> 

    • Every time I press the button, I need to re-display a random number and not at the end of the page, but where I’ll write document.write (). Or how Text Node can be displayed among the desired tags in the middle of the html document? - Roman Kravets
    • one
      @RomanKravets changed the code. There are minor cosmetic changes, but the essence is the same. - Regent
    • Earned, thanks. - Roman Kravets