Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
how to separate buttons in html table

I tried to add buttons at the front of each row, but only the one in the first row would follow my setColor() function.

while(($row=mysqli_fetch_assoc($result))&&($cnt<5)){
          if($row['type']=='0') {
              $cal+=$row['kcal'];
              $price+=$row['price'];
              if(($cal<=($_SESSION['calorie'])/3)){
                  echo"<tr>
                  <td>
                      <form>
                      <input type='button' value='+' onclick='setColor()' id='btn'>
                      </form></td>
                      <td>".$row['product']."</td>
                      <td>".$cal."</td>
                      <td>".$price."</td>
                      </tr>";
                  $cnt+=1;
              }
          $price=0;
          $cal=0;}
      }

here is the setColor JS function code

<script>
function setColor(){
    var count=1;
    if (count == 0){
        document.getElementById('btn').style.color = "gray";
        count=1;        
    }
    else{
        document.getElementById('btn').style.color = "orange";
        count=0;
    }
    count=0
}

</script>

How can I separate them while keeping the while loop? Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to let the system know which button you want to change on clicking the button , one of the methods is to use a unique button ID.

Hence simply change to

$index=0;

while(($row=mysqli_fetch_assoc($result))&&($cnt<5)){
          if($row['type']=='0') {
              $cal+=$row['kcal'];
              $price+=$row['price'];
              if(($cal<=($_SESSION['calorie'])/3)){
                  echo"<tr>
                  <td>
                      <form>
                      <input type='button' value='+' onclick='setColor(". $index . ")' id='btn". $index . "'>
                      </form></td>
                      <td>".$row['product']."</td>
                      <td>".$cal."</td>
                      <td>".$price."</td>
                      </tr>";
                  $cnt+=1;
              }
          $price=0;
          $cal=0;}
$index++;
      }

and use the following JS

<script>
function setColor(var1){
    var count=1;
    if (count == 0){
        document.getElementById('btn'+var1).style.color = "gray";
        count=1;        
    }
    else{
        document.getElementById('btn'+var1).style.color = "orange";
        count=0;
    }
    count=0
}

</script>

For the JS code, I do not know what is the purpose of the "count", so you may wish to amend the code to suit your need.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda