TheWalkingPlay2500 TheWalkingPlay2500 - 1 year ago 47
Javascript Question

Disabling an input when a checkbox is checked

I need to disable a cmp when a checkbox is checked, I tried this:

<script language="text/javascript">

var x = document.getElementById("SemPretencao").checked;

function desativar() {

if (x==true) {
alert("Seu Babacaum");
} else {


document.getElementById("SemPretencao").addEventListener("click", desativar();, false);



<a class="field_names">Pretenção Salarial: </a> <input type="number" class="number_inputs" name="salario" id="salario"> <br>
<a class="field_names">Não Tenho Pretenção Salarial </a> <input type="checkbox" class="checkbox_input" name="SemPretencao" id="SemPretencao">

Sorry for my English, I'm Brazilian.

Answer Source

Your addEventListener is wrong. You're calling the function instead of passing it. Your function can also be simplified a bit. Try this:

function desativar() {
  document.getElementById("salario").disabled = document.getElementById("SemPretencao").checked;

document.getElementById("SemPretencao").addEventListener("click", desativar, false);

Working example