Pass and receive field id by parameter

Asked

Viewed 3,493 times

1

I have this html code:

<input type="radio" name="resultado" value="Y" onclick="habilitaCampo(this.value)">Aprovado 
<input type="radio" name="resultado" value="N" onclick="habilitaCampo(this.value)">Recusado<br> 
<input type="text" id="data_validade" name="data_validade" size="15" maxlength="10" disabled="disabled" onKeyPress="mascara_data(this,event);"/>

And the javascript function:

function habilitaCampo(valor){  
  if(valor == "Y"){     
    document.getElementById("data_validade").disabled = false;  
    document.getElementById("data_validade").focus();
  }
  else{     
    document.getElementById("data_validade").disabled = true;           
  }
}   

to enable or disable the field expiration date. But I wanted to pass the id of the field as parameter and be able to use this function in other fields, because I am creating several equal and unnecessary functions, and I can’t pass and receive the value correctly. Can you help me in this matter?

  • Which field you want to cross? data_validade?

  • And because you do not pass the value once in the function: onclick="enable')"?

  • @Sergio that field right!

  • @Aline the idea would be this, but is giving error Uncaught Referenceerror: habilitaCampo is not defined at Htmlinputelement.onclick (helpers.php:86) 86 being the radio input line.

2 answers

2


In the case of the HTML you have, you could simplify and send the state directly once each radio has its own function invocator.

function habilitaCampo(estado, id) {
  var el = document.getElementById(id);
  el.disabled = !estado;
  if (estado == true) el.focus();
}
<input type="radio" name="resultado" onclick="habilitaCampo(true, 'data_validade')">Aprovado
<input type="radio" name="resultado" onclick="habilitaCampo(false, 'data_validade')">Recusado<br>
<input type="text" id="data_validade" name="data_validade" size="15" maxlength="10" disabled="disabled" onKeyPress="mascara_data(this, event);" />

This implies that the function habilitaCampo is accessible to the global scope, ie. which you have for example in the HTML file inside a tag <script> and outside any other function.


Another way, since you say you are repeating code, would be to do without Javascript inline. With .addEventListener in an organized way to save code:

function ativador(nome, id) {
  var el = document.getElementById(id);
  [].forEach.call(document.getElementsByName(nome), function(radio) {
    radio.addEventListener('click', habilitaCampo.bind(radio, el));
  });
}

function habilitaCampo(el) {
  el.disabled = this.value == 'N';
  if (!el.disabled) el.focus();
}

ativador('resultadoA', 'data_validadeA');
ativador('resultadoB', 'data_validadeB');
input {
  display: block;
}
<input type="radio" name="resultadoA" value="Y">Aprovado
<input type="radio" name="resultadoA" value="N">Recusado<br>
<input type="text" id="data_validadeA" name="data_validade" size="15" maxlength="10" value="10-5-1998" disabled="disabled" onKeyPress="mascara_data(this,event);" />

<hr>

<input type="radio" name="resultadoB" value="Y">Aprovado
<input type="radio" name="resultadoB" value="N">Recusado<br>
<input type="text" id="data_validadeB" name="data_validade" size="15" maxlength="10" value="10-5-1998" disabled="disabled" onKeyPress="mascara_data(this,event);" />

  • Sérgio, here worked correctly, but in my code I didn’t want to work. I will continue testing here. Thanks.

  • @thiagofred that mistake "habilitaCampo is not defined " tells me you have that function within another. A onload = function() {} or .ready(function(){}. That is the case?

  • @thiagofred put together another way to do this.

  • Sergio, not just another function in the field of expiration date to mask in xx/xx/xxxx format

  • @thiagofred can show how the whole HTML is from one end to the other?

  • I just redid the code with the three fields you sent me in a new file, and the error continued to occur in Chrome. While testing in Internet Explorer the error did not happen. It is only incompatibility or there is another error not observed?

Show 2 more comments

1

So I believe it should work Thiago:

    function habilitaCampo(valor, id){  
        if(valor == "Y"){     
          document.getElementById(id).disabled = false;  
          document.getElementById(id).focus();
        }
        else{     
          document.getElementById(id).disabled = true;           
        }
    } 
<input type="radio" name="resultado" value="Y" onclick="habilitaCampo(this.value, 'data_validade')">Aprovado 
    <input type="radio" name="resultado" value="N" onclick="habilitaCampo(this.value, 'data_validade')">Recusado<br> 
    <input type="text" id="data_validade" name="data_validade" size="15" maxlength="10" disabled="disabled" onKeyPress="mascara_data(this,event);"/>

Edited: I switched to code block for better viewing and testing.

  • Diego, in Chrome developer mode, gives the following error: Uncaught Referenceerror: habilitaCampo is not defined at Htmlinputelement.onclick (helpers.php:86) , being the 86 line, the input type radio field line.

  • Thiago, check the copied code, edit the publication for the code block mode for better viewing and it’s correct.

  • Diego, really here it worked, on my page it didn’t work, I copied the code and pasted there, but it didn’t work. I will do other tests. Thank you.

  • OK, good luck. Anything calls me. Hugs

Browser other questions tagged

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