I have a doubt regarding the conditions within a querySelector, what I am trying to do is show or not a button that I generate from a for loop as shown below:
function LlenarTabla(){
document.querySelector('#tabla_docente tbody').innerHTML = '<tr> </tr>';
for (let i = 0; i < contratos.length; i++)
document.querySelector('#tabla_docente tbody').innerHTML += `
<tr>
<td style='visibility:collapse; display:none;'> ${contratos[i].id_cont} </td>
<td>${i+1} </td>
<td> ${contratos[i].nombre} </td>
<td> ${contratos[i].nombre_tipo}</td>
<td>
<div class="form-group">
<form action='./reportes/reporte_${contratos[i].c_tipo}a.php' method='POST' target='_blank'>
<input type='hidden' name='docente_ciclo' value='${contratos[i].c_ciclo}'>
<input type='hidden' name='docente_dep' value='${contratos[i].c_depe}'>
<input type='hidden' name='docente_id' value='${contratos[i].c_doc}'>
<input type='hidden' name='tipo' value='${contratos[i].c_tipo}'>
<input type='submit' name='' value='generar' class='btn btn-primary'>
</form>
</div>
</td>
<td>
<button type="button" onclick="desc_asig(${contratos[i].id_cont})" class="btn btn-outline-dark">
Titular
</button>
</td>
</td>
</tr>
`;
}
I intended to place a condition in the middle of the querySelector,
if(contratos[i].c_tipo==2){
`<td>
<button> </button>
</td>`
}
but this does not allow me (or I have not deciphered how), I would greatly appreciate your help
Try something like this:
where what we do to insert the code you need is simply close the current string with the backtick (grave), then the plus symbol + and the variable that contains the result of the if you need to do, to finish with another plus symbol + and then we open the text string again with the backtick:
Using this syntax you could do it in many other ways, although what I have done is create the button variable to make it cleaner and thus not put the conditional between the addition symbols.