Problem adding a value to a variable

0

I have the following code:

if(filas[i] == filas[0]) { // donde filas es = 0 que es el encabezado de mi tabla

      for(var k=0; k <celdas.length; k++){ // donde celdas son el numero de columnas
        s = celdas[k].textContent;
      }        
  } 

what it does is go through the number of cells that a table has and recover the value it has in the part of the header

the table would have for example

  

| name | age | genre | numHijos |

and every time you go through the number of cells the value of that can be

s= nombre;
s = edad;
s= genero;
s=numHijos;

and so to be able to send the parameter, the for if the header goes through the problem is that the only thing it does is put the last value in this case

s=numHijos;

Here all the code I use

function tabla(){
        var tabla = document.getElementById("datos");
        var filas = tabla.getElementsByTagName("tr"), celdas;
        var suc=0;
        var p=0;
        var s=0;
          for (var i = 0; i < filas.length; i++){
            celdas = filas[i].children;

              for (var j = 0; j < celdas.length; j++){
                let celda_actual = celdas[j];
                  if(celda_actual.textContent == "-"){
                    celda_actual.id = "f"+i+"- c"+j; 
                    celda_actual.className = "cuadro";
                  }else{
                    celda_actual.id = ""; 
                    celda_actual.className = "";
                  }    
                  if(celdas[1]){
                    var m = celdas[1].textContent;
                  }
                  if(celdas[2]){
                    p =celdas[2].textContent
                  }

                  if(filas[i] == filas[0]) {

                    s = celdas[j].textContent;
                  }        

                  if(celda_actual.textContent == "-" ){
                    celda_actual.textContent = "[f"+i+"- c"+j+"]"+"[m="+m+", p="+p+", s="+s+"]";
                  }  
                }
          }
    }

Someone who can help me solve it?

    
asked by Soldier 03.08.2017 в 17:50
source

1 answer

0

You must concatenate the variable s since you are overwriting it.

Also do not initialize it, since you are saying that it is of an integer type when declaring s = 0;

if(filas[i] == filas[0]) {

      s += celdas[j].textContent + '&';
}

You can add a separator. Just remember when sending this variable, remove the last separator like this:

s = s.substring(0, s.length-1);

Greetings.

    
answered by 03.08.2017 / 18:23
source