Hello! I am only at the beginning of the study of js))) Therefore, this question: There are 5 hidden blocks (display: none) and a button with an event attached to it. The crux of the matter: when you press a button, a random counter from 0 to 5 is turned on. And depending on the number dropped out, the block with resp. id is assigned the display: block style. Those. he appears. Tell me how to make, so that when you press the button again, the visible block hides or reappears if the number corresponding to its id appears. Html code:
<div class="container" id="randCont"> <button id="randing" onclick="rand()">Узнать судьбу</button> <div id="one"> <h1>Первый</h1> <p>Первый текст</p> </div> <div id="two"> <h1>Второй</h1> <p>Второй текст</p> </div> <div id="three"> <h1>Третий</h1> <p>Третий текст</p> </div> <div id="four"> <h1>Четвертый</h1> <p>Четвертый текст</p> </div> <div id="five"> <h1>Пятый</h1> <p>Пятый текст</p> </div> </div> Js code:
var doc, total, div_R, btn; doc = document; div_R = doc.querySelector("#randCont div"); btn = doc.getElementById('randing'); function rand() { total = Math.round(Math.random() * 5); switch (total) { case 1: doc.getElementById('one').style.display = 'block'; break case 2: doc.getElementById('two').style.display = 'block'; break case 3: doc.getElementById('three').style.display = 'block'; break case 4: doc.getElementById('four').style.display = 'block'; break case 5: doc.getElementById('five').style.display = 'block'; break }; doc.getElementById('randing').innerText = "Попробовать ещё"; };