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

158
Vistas
Save the values of this javascript

Well I have this code that changes the colors of a cell, but what I'm looking for is that it doesn't delete the results when I restart the page, because I'm making a page so that users can mark the hours they have available weekly.

I don't know if someone can help me, to save those values, thanks.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>Untitled</title>
   <script language="javascript" type="text/javascript">
   function ilumina(celda){
    if (celda.style.backgroundColor=="yellow")
        {
        celda.style.backgroundColor="green";
        }
    else
        {
        celda.style.backgroundColor="yellow";
        }
    }
  </SCRIPT>
  </head>
  <body>
    <table border="1" width="50%">
        <tr>
            <td bgcolor="green" onclick="ilumina(this)">&nbsp;</td>
            <td bgcolor="green" onclick="ilumina(this)">&nbsp;</td>
            <td bgcolor="green" onclick="ilumina(this)">&nbsp;</td>
            <td bgcolor="green" onclick="ilumina(this)">&nbsp;</td>
        </tr>
        
  </table>
  </body>
</html>

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

0

You can use

  1. arrays, to save each elements color value
  2. JSON encode / decode, to convert arrays to strings-text (needed to save to localstorage, since it's used to save strings-text
  3. localstorage set / get

You can save your data each time you change the colors, then on load of the page you can check if you have previously saved any data. If yes, then load the data and change the colors.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>Untitled</title>
   <script language="javascript" type="text/javascript">
   
    //DOMContentLoaded = when page elements are loaded
    document.addEventListener("DOMContentLoaded", function(event) {
        //check if we have saved any 'celda_storage_localstorage' value
        if ( localStorage.getItem('celda_storage_localstorage') != null ) {
            celda_storage = JSON.parse( localStorage.getItem('celda_storage_localstorage') );
            
            //load colors from celda_storage
            for (let i=0; i < celda_storage.length; i++) {
                document.querySelectorAll('#celdas_table td')[i].style.backgroundColor = celda_storage[i];
            }
        }
    });
   
    function ilumina(celda){
        if (celda.style.backgroundColor=="yellow")
            {
            celda.style.backgroundColor="green";
            }
        else
            {
            celda.style.backgroundColor="yellow";
            }
            
        //save colors to celda_storage
        for (let i=0; i < document.querySelectorAll('#celdas_table td').length; i++) {
            celda_storage[i] = document.querySelectorAll('#celdas_table td')[i].style.backgroundColor;
        }
        
        console.log(celda_storage);
        
        //JSON.stringify = array to string
        localStorage.setItem('celda_storage_localstorage',JSON.stringify(celda_storage));
        //JSON.parse = string to array
        console.log( JSON.parse( localStorage.getItem('celda_storage_localstorage') ) );
    }
  </SCRIPT>
  </head>
  <body>
    <table id="celdas_table" border="1" width="50%">
        <tr>
            <td style="background-color : green" onclick="ilumina(this)">&nbsp;</td>
            <td style="background-color : green" onclick="ilumina(this)">&nbsp;</td>
            <td style="background-color : green" onclick="ilumina(this)">&nbsp;</td>
            <td style="background-color : green" onclick="ilumina(this)">&nbsp;</td>
        </tr>
        
  </table>
  </body>
</html>

Have fun and ask !

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I do it with localstorage, it only saves the information for my browser. I think I need to send the information to the backend, but with my knowledge I don't know how to do that.

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