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

243
Vistas
Edit the styling of a button (PHP & MySQL used to populate it) using Javascript

Good day,

I am building an e-commerce site.

I have used MySQL for the back-end database and PHP to populate the data and tables into my Website.

I have managed a way that when a product is succesfully sold that the innerText of that button (that leads to it's detailed page) changes from "View" to "SOLD".

I now want to use JavaScript to scan through the table and Find all the button that contain the word "SOLD" and change the styling of them.

*I think my main problem is that the data is puled through from the MySQL database and isn't 'fixed' into the HTML, which is why I think my code is not working.

Here are snippets of my relevant code: This is the "button" that I want to change, if required condition is met.

<td id="view_button"><?php echo "<a id='isSold' href='/dynamic_page.php?id={$row['id']}'>{$row['View']}</a>"; ?></td>

Here is my attempt at the JavaScript:

const isSold = document.getElementById('isSold');

function ToggleSold(){
  if(isSold.innerHTML === "SOLD"){
    this.style.backgroundColor = "grey";
  } else {
    this.style.backgroundColor = "";
  }
}

Please bare with me, I taught myself how to code over the last 6 months. All help is appreciated!

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

0

I took the advice of the above comment to use PHP instead of JavaScript. It has done the trick.

I'll post the code here, in case someone has the same issue:

<td id="view_button">
    <a style="<?php if ($row['View'] == 'SOLD') {
        echo 'color:rgb(128, 128, 128);
              background: rgb(217, 217, 217);
              border: 2px solid rgb(128, 128, 128);
              font-size: 10px';

    } else if($row['View'] == 'View') {
        ;
    } ?>" href="<?php if($row['View'] == 'SOLD') {
        echo '';
    } else if($row['View'] == 'View') {
        echo "/dynamic_page.php?id={$row['id']}";
    } ?>">
        <?php echo $row['View'] ?>
    </a>
</td> 
     
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