Résolu Récupération de valeurs js/html

Vatinj-NayLeeQ SEE

☼ [GFX 2D] ☼
Premium
Inscription
29 Septembre 2011
Messages
388
Réactions
204
Points
20 088
Bonjour à tous !

Voilà mon problème est le suivant j'ai une suite de boutons chacun de ces boutons vont de 1 à 20.
j'ai deux textbox une qui affiche la valeur sur laquelle on a cliqué et l'autre qui indique un plus ou moins, je n'arrive pas à récupérer la valeur du bouton et à la comparer à celle enregistrée par l'ordinateur.
HTML:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Mon Big Guess</title>
    <link rel="stylesheet" type="text/css" href="flowbigguess.css">   
    <script type="text/javascript" src="codebigguess.js"></script>
</head>
<body>
    <h1 id="titre">Mon Big Guess</h1>
    <div id="tableau">
    <input type="text" id="afficheurleft">
    <input type="text" id="afficheurright">
    <button type="button" class="touche" onclick="affiche('0')">0</button>
    <button type="button" class="touche" onclick="affiche('1')">1</button>
    <button type="button" class="touche" onclick="affiche('2')">2</button>
    <button type="button" class="touche" onclick="affiche('3')">3</button>
    <button type="button" class="touche" onclick="affiche('4')">4</button>
    <button type="button" class="touche" onclick="affiche('5')">5</button>
    <button type="button" class="touche" onclick="affiche('6')">6</button>
    <button type="button" class="touche" onclick="affiche('7')">7</button>
    <button type="button" class="touche" onclick="affiche('8')">8</button>
    <button type="button" class="touche" onclick="affiche('9')">9</button>
    <button type="button" class="touche" onclick="affiche('10')">10</button>
    <button type="button" class="touche" onclick="affiche('11')">11</button>
    <button type="button" class="touche" onclick="affiche('12')">12</button>
    <button type="button" class="touche" onclick="affiche('13')">13</button>
    <button type="button" class="touche" onclick="affiche('14')">14</button>
    <button type="button" class="touche" onclick="affiche('15')">15</button>
    <button type="button" class="touche" onclick="affiche('16')">16</button>
    <button type="button" class="touche" onclick="affiche('17')">17</button>
    <button type="button" class="touche" onclick="affiche('18')">18</button>
    <button type="button" class="touche" onclick="affiche('19')">19</button>
    <button type="button" class="touche" onclick="affiche('20')">20</button>
    
</div>

</body>
</html>

Code:
var rand = Math.floor(Math.random()*20+1);
var tours = 0;
var nombre;
var a;

document.getElementById('afficheurleft').addEventListener('onclick', nombre);

function nombre() {

    nombre = document.getElementById('button').value;

    //parseInt(nombre);

    if (nombre === rand) {
        alert('T\'as gagné en '+ tours +' tours  fdp');
    } else if (nombre < rand) {
        alert('C\'est + pd');
        tours++;
    } else if (nombre > rand) {
        alert('C\'est - enculé');
        tours++;
    } else {
        alert('C\'est pas un nombre putain.');
    }
}

/* function affiche(par)
{
    a = document.getElementById("afficheurleft");

    if(par=='c'){
        a.value='';
    }
    else {
     a.value=a.value+par;
    }
    console.log(affiche);
} */
 

Rivals

Ancien staff
Inscription
27 Août 2016
Messages
1 705
Réactions
895
Points
13 104
Code:
<html>
<head>
    <meta charset="utf-8">
    <title>Mon Big Guess</title>
    <link rel="stylesheet" type="text/css" href="flowbigguess.css">
</head>
<body>
    <h1 id="titre">Mon Big Guess</h1>
    <div id="tableau">
    <input type="text" id="afficheurleft">
    <input type="text" id="afficheurright">
    <button type="button" class="touche" onclick="affiche(0)">0</button>
    <button type="button" class="touche" onclick="affiche(1)">1</button>
    <button type="button" class="touche" onclick="affiche(2)">2</button>
    <button type="button" class="touche" onclick="affiche(3)">3</button>
    <button type="button" class="touche" onclick="affiche(4)">4</button>
    <button type="button" class="touche" onclick="affiche(5)">5</button>
    <button type="button" class="touche" onclick="affiche(6)">6</button>
    <button type="button" class="touche" onclick="affiche(7)">7</button>
    <button type="button" class="touche" onclick="affiche(8)">8</button>
    <button type="button" class="touche" onclick="affiche(9)">9</button>
    <button type="button" class="touche" onclick="affiche(10)">10</button>
    <button type="button" class="touche" onclick="affiche(11)">11</button>
    <button type="button" class="touche" onclick="affiche(12)">12</button>
    <button type="button" class="touche" onclick="affiche(13)">13</button>
    <button type="button" class="touche" onclick="affiche(14)">14</button>
    <button type="button" class="touche" onclick="affiche(15)">15</button>
    <button type="button" class="touche" onclick="affiche(16)">16</button>
    <button type="button" class="touche" onclick="affiche(17)">17</button>
    <button type="button" class="touche" onclick="affiche(18)">18</button>
    <button type="button" class="touche" onclick="affiche(19)">19</button>
    <button type="button" class="touche" onclick="affiche(20)">20</button>

</div>

</body>
<script type="text/javascript" src="codebigguess.js"></script>
</html>

Code:
//game
var randNumber = getRandNumber();
var tours = 0;

//elements
var displayerLeft = document.getElementById('afficheurleft');


function affiche(number)
{

  //si le nombre est supérieur à celui généré
  if(number > randNumber)
  {
    console.log('inférieur');
    displayerLeft.value = "Inférieur";
  }

  //si le nombre est inférieur à celui généré
  if(number < randNumber)
  {
    console.log('supérieur');
    displayerLeft.value = "Supérieur";
  }

  //si le nombre est égal à celui généré #WIN
  if(number == randNumber)
  {
    tours += 1;
    alert('Tu as gagné ! Nombre de tours : ' + tours);
    getRandNumber();
  }
}

function getRandNumber()
{
  randNumber = Math.floor(Math.random()*20+1);
  document.getElementById('afficheurright').value = randNumber;
  return randNumber;
}
 
Cette réponse a aidé l'auteur de cette discussion !
Haut