Disabled does not work Javascript

2

I have the following code to unlock a button, but it does not work, What is the error ?, which means that the error only appears in the google chrome console and not in the errors for example of liveweave.com?

ERROR:

  

uParty is not a function       at HTMLInputElement.onclick

<!DOCTYPE html>
<html>

<head>
  <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>

<body>
  <input id="partyCode" placeholder="Party code" type="text" disabled>
  <input onclick="uParty()" type="button" id="uParty" value="Unirse a party">
  <script>
    $(document).ready(function() {
      function uParty() {
        document.getElementById('partyCode').disabled = false;
      }
    });
  </script>

</body>

</html>
    
asked by Eduardo Sebastian 10.05.2017 в 05:28
source

3 answers

2

It is not understood if you want to use Jquery or "bare" Javascript; you are mixing the two alternatives.

In "bare" Javascript it would be:

function habilitar() {
          document.getElementById('partyCode').disabled = false;
}
<input id="partyCode" placeholder="Party code" type="text" disabled>
<input type="button" id="uParty" value="Unirse a party" onclick="habilitar()">

With Jquery it would be like this:

$("#uParty").click( function() {
   $("#partyCode").prop('disabled', false);
 }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input id="partyCode" placeholder="Party code" type="text" disabled>
<input type="button" id="uParty" value="Unirse a party" >

Of course, it is important that you understand what happens in each case ...

By the way, your original code simply walks if you get the function out of all that jquery initialization block

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input id="partyCode" placeholder="Party code" type="text" disabled>
<input onclick="uParty()" type="button" id="uParty" value="Unirse a party">
<script>
 function uParty() {
            document.getElementById('partyCode').disabled = false;
        }

</script>

</body>
</html>

Your original code, what I was doing was, at the moment of being ready the page, running a certain (anonymous) function which only declared a function uParty (and remember that declaring a function in Javascript is like instantiate an object). But that function uParty was declared / instantiated as a local object of the anonymous function (and it was not invoked or assigned or anything), so that when the anonymous function was finished, there were no traces of uParty . You, on the other hand, wanted uParty to be available in the global environment (that in Javascript hosted on the website corresponds to the object window ). Therefore, you could if you want to do this:

$(document).ready(function(){
        window.uParty =  function () {
            document.getElementById('partyCode').disabled = false;
      }
   });

But it would be something twisted, and nothing necessary.

    
answered by 10.05.2017 / 05:54
source
1

I share a code that I had a very good test and if you enable and disable an input

when nfactura is my input and box is my combox

<script >

//con esta funcion haces que el combox habilite un text

var input = document.getElementById('nfactura');

function carg(elemento) {
  d = caja.value;

  if(d == "cf"){
    nfactura.disabled = false;

  }else{
    nfactura.disabled = true;
  }
}
</script>

in the combox

  <select id="caja" name="caja" onchange="carg(this);" >
      <option value="*">selecciona los datos</option>
      <option value="sf" >sin factura</option>
      <option value="cf" >con factura</option>
</select>
    
answered by 15.01.2019 в 16:27
0

I changed several things:

  • I put the label <script></script> in <head></head> .

  • I removed the function uParty from $(document).ready(function(){} , remaining loose.

  • The getElementById function, in my opinion, does not help, since you can call it directly.

<!DOCTYPE html>
<html>
  <head>
  <script>
  function uParty() {
    partyCode.disabled=false
  }
  </script>
  </head>
  <body>
    <input id="partyCode" placeholder="Party code" type="text" disabled>
    <input onclick="uParty()" type="button" id="uParty" value="Unirse a party">
  </body>
</html>
    
answered by 10.05.2017 в 13:51