Enable an input with a checkbox

0

please help me with this problem I want that when I select checkbox the input is activated, I want to do it with readonly because with disabled it does not save me in BD , with disabled if it works but with readonly no.

   function comprobar(obj)
{   
    if (obj.checked)
      document.getElementById('boton').readOnly = false;
        
    else
      document.getElementById('boton').readOnly = true;
        
}
<input name="chec" type="checkbox" id="chec" onChange="comprobar(this);"/>
    <label for="chec">Activar</label>
 
    <input name="text" id="boton" readonly />
    
asked by JOHAN ALEXIS MORENO GUISAO 17.08.2018 в 17:15
source

3 answers

1

Friend here I pass the code to achieve what you want with JavaScript

   function comprobar(obj)
{   
    if (obj.checked){
      
document.getElementById('boton').style.display = "";
   } else{
      
document.getElementById('boton').style.display = "none";
   }     
}
<input name="chec" type="checkbox" id="chec" onChange="comprobar(this);"/>
    <label for="chec">Activar</label>
 
    <input name="text" id="boton" readonly style="display:none" />
    
answered by 17.08.2018 / 17:46
source
1

function comprobar()
{   
    if (document.getElementById("chec").checked)
      document.getElementById('boton').readOnly = false;
        
    else
      document.getElementById('boton').readOnly = true;
        
}
<input name="chec" type="checkbox" id="chec" onchange="comprobar();"/>
    <label for="chec">Activar</label>
 
    <input name="text" id="boton" readonly />
    
answered by 17.08.2018 в 17:24
1

Friend I enclose my solution:

function comprobar()
{
  document.getElementById('boton').readOnly = !document.getElementById("chec").checked;
}
<input name="chec" type="checkbox" id="chec" onchange="comprobar();"/>
<label for="chec">Activar</label>
<input name="text" id="boton" readonly="true" />
    
answered by 17.08.2018 в 17:27