JSFiddle - React, Tailwind, and code Playground
by Radekzatec
HTML
<!DOCTYPE html>
<html lang="cs_CZ" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form class="form_hadej" name="form_hadej" action="index.html" method="post">
<legend><strong><em>Hádej číslo</em></strong></legend>
<div class="nastav">
<label for="c1">Vlož rozsah</label>
<input type="number" id="c1" class="cislo" onkeyup="aktivuj(this);" onchange="aktivuj(this);" name="cislo1" value="">
<button type="button" id="spust" onclick="hraj(this);" name="spust" disabled>Spusť</button>
</div>
<div id="rozsah">
</div>
<div id="hraj">
<label for="c1">Hádej</label>
<input type="number" id="c2" class="cislo" onkeyup="aktivuj(this, 'hadej');" onchange="aktivuj(this, 'hadej');" name="cislo2" value="">
<button type="button" id="hadej" onclick="tipuj(this);" name="hadej" disabled>Hádej</button>
</div>
<div id="vysledky">
</div>
</form>
<script type="text/javascript"src="main.js"></script>
</body>
</html>
CSS
#hraj, #rozsah, #vysledky {
visibility: hidden;
}
JavaScript
const cislo1 = document.getElementById('c1');
const cislo2 = document.getElementById('c2');
const spust = document.getElementById('spust');
const hadej = document.getElementById('hadej');
//const rndcislo = Math.floor(Math.random() * parseInt(cislo1.value) +1);
let pokusy = 0;
let aktivuj = (element, buttonid) => {
let val = parseFloat(element.value);
let btn = buttonid !== undefined ? document.getElementById(buttonid) : element.nextElementSibling;
btn.disabled = isNaN(val) ? true : false;
}
function skryt() {
document.getElementById('hraj').style.visibility = 'hidden';
document.getElementById('rozsah').style.visibility = 'hidden';
document.getElementById('vysledky').style.visibility = 'hidden';
spust.disabled = true;
}
function hraj() {
document.getElementById('hraj').style.visibility = 'visible';
document.getElementById('rozsah').style.visibility = 'visible';
//let rndcislo = Math.floor(Math.random() * parseInt(cislo1.value) +1);
if ((parseInt(cislo1.value)) <= 1) {
skryt();
alert('Zadané číslo musí být kladné a větší než jedna');
} else if ((parseInt(cislo1.value)) !='' || (parseInt(cislo1.value)) != 0) {
spust.disabled = true;
document.getElementById('rozsah').innerHTML = '<p>Rozsah hledaného čísla je od 1 do ' + (parseInt(cislo1.value)) + '.</p>';
} else {
skryt();
alert('Vyplňte hodnotu rozsahu hledaného čísla.');
}
//return rndcislo;
}
// console.log(hraj.rndcislo);
/*function nahodne_cislo() {
let rndcislo = Math.floor(Math.random() * parseInt(cislo1.value)) + 1;
console.log(rndcislo);
return rndcislo;
} */
let rndcislo = () => {
return Math.floor(Math.random() * parseInt(cislo1.value) +1);
}
function tipuj() {
console.log(cislo1.value);
console.log(cislo2.value);
console.log(rndcislo);
document.getElementById('vysledky').style.visibility = 'visible';
let nova_hra = '<div><button type="button"...