1

I have this simple form:

<form align="center" onload="chk()" name="form" action="" method="post" id="form">  
  <input type="text" name="quantt" id="quantt" value="">    
  <input type="text" name="avail" id="avail" value="">  
</form>

the "avail" input is auto-filled from a php script. My question is How can I hide or disable the "quantt" input if the "avail" value is empty on loading of the form ?

I've tried this function but it didn't work :-(

function chk() {
    var ava = document.form.avail;
    if (ava.value == "") {
        document.getElementById("quantt").type = "hidden";
    } else {
        document.getElementById("quantt").type = "text";
    }
}

Thank you so much for any help.

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
karmel
  • 57
  • 1
  • 9

3 Answers3

1

The chk() function seems to work fine. Try to call it using window.onload, like:

window.onload = function() {
    chk();
};

For more information refer to this question.

Community
  • 1
  • 1
Pedro Henrique
  • 601
  • 6
  • 17
0

Use below function

<script>
    function chk() 
    {
        if ( $("#avail").val() == "" ) {
            $("#quantt").attr("hide", true)
        } else {
            $("#quantt").attr("hide", false)
        }
    }
</script>
Vee Mandke
  • 578
  • 1
  • 11
  • 28
0

Using Element.querySelector() you can call your function chk on body load:

document.querySelector('body').onload = function chk() {
  var qte = document.getElementById('quantt')
  document.getElementById('avail').type = (qte.value === '') ? 'hidden' : 'text';
};
<form align="center" name="form" action="" method="post" id="form">  
  <input type="hidden" name="quantt" id="quantt" value="">    
  <input type="text" name="avail" id="avail" value="">    
</form>
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46