Enable select HTML with

Asked

Viewed 600 times

3

I have a table, where fields start disabled, and in a checkbox I choose which field to enable. Tagged input, works perfectly. But when I try to use with the select the same script does not work.

I tried to add this method only to the select. The same works, but loses the highlight of the line.

 document.getElementById('Sexo').onchange = function () {
        document.getElementById('TpSexo').disabled = !this.checked;
    };

Code in use:

    var cbs = document.getElementsByClassName('cb');

    function cbClick() {
        var input = document.querySelector('input[data-id="' + this.getAttribute('data-id') + '"]:not([type="checkbox"])');
        input.disabled = !this.checked;

        // parentNode.parentNode = td > tr subindo a hierarquia
        if (this.checked) {
            // muda a cor do fudo quando for marcado
            input.parentNode.parentNode.style.background = '#e1e1e1';
        } else {
            // remove a cor do fundo ao desmarcar
            input.parentNode.parentNode.style.background = ''; //
        }
    }

    for (var i in cbs) {
        cbs[i].onclick = cbClick;
    }
<table class="table">
  <thead>
    <tr>
      <td></td>
      <th>CAMPO</th>
      <th>INFORMAÇÃO ATUAL</th>
      <th>INFORMAÇÃO CORRETA</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
          <input type="checkbox" class="cb" data-id="DtNascimento" />
      </td>
      <td>Data de Nascimento</td>
      <td>11/11/1111</td>
      <td>
          <input type="text" data-id="DtNascimento" disabled class="form-control datapicker" name="DtNascimento" id="DtNascimento" />
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="cb" data-id="DsEstadoCivil">
      </td>
      <td>Estado Civil</td>
      <td>Solteiro</td>
      <td>
          <input type="text" data-id="DsEstadoCivil" disabled class="form-control" name="DsEstadoCivil" />
      </td>
    </tr>
   
   <tr>
        <td><input type="checkbox" class="cb" data-id="TpSexo"></td>
         <td> Sexo</td>
         <td>Feminino</td>
         <td>
         <select data-id="TpSexo" disabled class="form-control" name="TpSexo">
        	 <option value=" "> </option>
       	 	 <option value="F">Feminino</option>
        	 <option value="M">Masculino</option>
    	 </select>
        	 </td>
	</tr>
  </tbody>
</table>

2 answers

3


Using the querySelectorAll, return all the elements that have the data-id, as in your case there are always two just use index 1 that represents the second element.

var cbs = document.getElementsByClassName('cb');

    function cbClick() {
        var input = document.querySelectorAll('[data-id="' + this.getAttribute('data-id') +'"]')[1];
        input.disabled = !this.checked;

        // parentNode.parentNode = td > tr subindo a hierarquia
        if (this.checked) {
            // muda a cor do fudo quando for marcado
            input.parentNode.parentNode.style.background = '#e1e1e1';
        } else {
            // remove a cor do fundo ao desmarcar
            input.parentNode.parentNode.style.background = ''; //
        }
    }

    for (var i in cbs) {
        cbs[i].onclick = cbClick;
    }
<table class="table">
  <thead>
    <tr>
      <td></td>
      <th>CAMPO</th>
      <th>INFORMAÇÃO ATUAL</th>
      <th>INFORMAÇÃO CORRETA</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
          <input type="checkbox" class="cb" data-id="DtNascimento" />
      </td>
      <td>Data de Nascimento</td>
      <td>11/11/1111</td>
      <td>
          <input type="text" data-id="DtNascimento" disabled class="form-control datapicker" name="DtNascimento" id="DtNascimento" />
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="cb" data-id="DsEstadoCivil">
      </td>
      <td>Estado Civil</td>
      <td>Solteiro</td>
      <td>
          <input type="text" data-id="DsEstadoCivil" disabled class="form-control" name="DsEstadoCivil" />
      </td>
    </tr>
   
   <tr>
        <td><input type="checkbox" class="cb" data-id="TpSexo"></td>
         <td> Sexo</td>
         <td>Feminino</td>
         <td>
         <select data-id="TpSexo" disabled class="form-control" name="TpSexo">
        	 <option value=" "> </option>
       	 	 <option value="F">Feminino</option>
        	 <option value="M">Masculino</option>
    	 </select>
        	 </td>
	</tr>
  </tbody>
</table>

  • 1

    It worked perfectly. And thank you for the explanation.

2

We had to select the select in function querySelector of cbClick:

var cbs = document.getElementsByClassName('cb');

    function cbClick() {
        var input = document.querySelector('input[data-id="' + this.getAttribute('data-id') + '"]:not([type="checkbox"]), select[data-id="' + this.getAttribute('data-id') + '"]:not([type="checkbox"])');
        input.disabled = !this.checked;

        // parentNode.parentNode = td > tr subindo a hierarquia
        if (this.checked) {
            // muda a cor do fudo quando for marcado
            input.parentNode.parentNode.style.background = '#e1e1e1';
        } else {
            // remove a cor do fundo ao desmarcar
            input.parentNode.parentNode.style.background = ''; //
        }
    }

    for (var i in cbs) {
        cbs[i].onclick = cbClick;
    }
<table class="table">
  <thead>
    <tr>
      <td></td>
      <th>CAMPO</th>
      <th>INFORMAÇÃO ATUAL</th>
      <th>INFORMAÇÃO CORRETA</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
          <input type="checkbox" class="cb" data-id="DtNascimento" />
      </td>
      <td>Data de Nascimento</td>
      <td>11/11/1111</td>
      <td>
          <input type="text" data-id="DtNascimento" disabled class="form-control datapicker" name="DtNascimento" id="DtNascimento" />
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="cb" data-id="DsEstadoCivil">
      </td>
      <td>Estado Civil</td>
      <td>Solteiro</td>
      <td>
          <input type="text" data-id="DsEstadoCivil" disabled class="form-control" name="DsEstadoCivil" />
      </td>
    </tr>
   
   <tr>
        <td><input type="checkbox" class="cb" data-id="TpSexo"></td>
         <td> Sexo</td>
         <td>Feminino</td>
         <td>
         <select data-id="TpSexo" disabled class="form-control" name="TpSexo">
        	 <option value=" "> </option>
       	 	 <option value="F">Feminino</option>
        	 <option value="M">Masculino</option>
    	 </select>
        	 </td>
	</tr>
  </tbody>
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.