You can not turn off Input using JavaScript

0

Friends a help, I can not deactivate input , I tried both ways:

$('input[name=txtApepaterno').val("prueba");
$( "#txtApepaterno" ).prop( "disabled", true );
document.getElementById("txtApepaterno").disabled = true;

If it fills me but does not deactivate, it should be noted that the input is created with dynamic html Javascript.

    
asked by Juan Seferino 06.09.2018 в 19:42
source

2 answers

0

I have some observations about your script.

Here you have to close with ']'

$('input[name=txtApepaterno').val("prueba");

I notice that first you want to search by input name and then to turn it off you search for id, this will only work unless the input has the same name and id, but I think that the error may come from there.

I take the time to make a functional example for the case you are commenting on.

function setValue() {
  $('input[name=txtApepaterno]').val("prueba");
}

function usingProp() {
  $("#txtApepaterno" ).prop( "disabled", true );
}

function usingGetElementById() {
  document.getElementById("txtApepaterno").disabled = true;
}

function activeInput(){
  $( "#txtApepaterno" ).prop( "disabled", false );
}
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  </head>
<body>
  

  <input id="txtApepaterno" name="txtApepaterno" />

  <ul>
  <li><a href="javascript:setValue();"> Setear valor </a></li>
  <li><a href="javascript:usingProp();"> Desactivar usando Prop </a></li>
  <li><a href="javascript:usingGetElementById();"> Desactivar usando getElementById </a></li>
  <li><a href="javascript:activeInput();"> Volver a activar </a></li>
  </ul>
</body>
</html>
    
answered by 06.09.2018 в 20:37
0

Here I leave an example with jquery, I hope you serve and can guide you

HTML

<form>
  <input id="txtApepaterno" type="text"/>
  <button id="disableit">Deshabilitar</button>
  <button id="enableit">Habilitar</button>
</form>

JS

$(document).ready(() => {

  $("#disableit").on('click',() => {
      $("#txtApepaterno").prop( "disabled", true );
  });

  $("#enableit").on('click',() => {
      $("#txtApepaterno").prop( "disabled", false );
  });

});

$(document).ready(() => {
	
  $("#disableit").on('click',() => {
      $("#txtApepaterno").prop( "disabled", true );
  });
  
  $("#enableit").on('click',() => {
      $("#txtApepaterno").prop( "disabled", false );
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <input id="txtApepaterno" type="text"/>
  <button id="disableit">Deshabilitar</button>
  <button id="enableit">Habilitar</button>
    
answered by 06.09.2018 в 20:58