Change the value of a hidden embedded in the column of a table

1

I have a way in HTML and JavaScript . I need by code to assign to each line of a table, which have status 1 , a subaddress through a combo (resolved this part).

The problem is that I need to duplicate any row using a button so that this same line duplicated (resolved), has status 2 to be able to differentiate them (unresolved part). I use a hidden where I save the status and it is embedded in the column of a table.

The problem is that I can not find how to reference the row where I press the duplicate button to change its value:

The button calls this function JavaScript :

function duplica_numeral(nI,numeral_id,of_au_id,au_id){

    //alert("Entro a duplicar numeral "  );
    var oTabla  = document.getElementById("Tab_Numerales");
    var nFilas  = oTabla.rows.length;
    var row     = document.getElementById("numeral"+nI); // find row to copy
    var clone   = row.cloneNode(true); // copy children too
    clone.id    = "numeral"+nFilas; // change id or other attributes/contents
    clone.getElementsByTagName('select')[0].id = "comboAtencion" + nFilas;
    alert("Before change to c ( hidNumeral has value 'a' "  ); // This line runs
    clone.getElementById('hidNumeral').value = 'c'; // this line is incorrect, why ??
    alert("after change to c "  ); // This line does not run  
}

HTML Code:

<table width="100%" border="0" cellpadding="0" id="Tab_Numerales" >
<tr height="25%" bgcolor="<?php echo $vcolor; ?>" 
 name="numeral<?echo$i;?>" id="numeral<?echo$i;?>">
    <td align="center" class="rows"><?php echo $i; ?></td>
    <td align="center" class="rows"><?php echo $row['numeral']; ?></td>
    <td align="center" class="rows"><input name="hidNumeral" type="text" id="hidNumeral" 

I want to put table hidnumeral in status 2 .

    
asked by Miguel Angel Bolaños 03.01.2018 в 02:17
source

1 answer

0

You must use JavaScript for it, specifically setAttribute() on the cloned element. Using your code as an example:

function duplica_numeral(nI,numeral_id,of_au_id,au_id){
  var oTabla  = document.getElementById("Tab_Numerales");
  var nFilas  = oTabla.rows.length;
  var clone   = document.getElementById("numeral"+nI).cloneNode(true);
  clone.id    = "numeral"+nFilas;
  console.log(clone);
  clone.children[2].children[0].value = '2';  //Asigna valor al INPUT
  clone.setAttribute("hidnumeral", nI + 1); //Asigna valor a la propiedad
  console.log(clone);
}

I recommend using console.log() instead of alert() since it gives you all the definition of the object you send to log and alert would only return the string [object] or similar.

Here I add a minimum and verifiable example:)

var referencia = document.getElementById("numeral_1");
var tablaPadre = referencia.parentNode
var clone = referencia.cloneNode(true);
clone.id = "numeral" + 2;
clone.children[1].children[0].value = 'MAMBO 2!!';
tablaPadre.appendChild(clone);
<table width="100%" border="0" cellpadding="0" id="Tab_Numerales">
  <tr height="25%" bgcolor="pink" name="numeral1" id="numeral_1">
    <td align="center" class="rows">1</td>
    <td align="center" class="rows"><input name="hidNumeral" type="text" id="hidNumeral_1" value="Mambo!" /></td>
  </tr>
</table>
    
answered by 03.01.2018 в 05:25