1

Guys please help me i cant do this :(. I want to disable my submit button until all the fields have values.. how can I do that?

this is my HTML codes

<table>
    <tr>
        <td>First Name: <input type="text"></td>
    </tr>
    <tr>
        <td>Middle Name: <input type="text"></td>
    </tr>
    <tr>
        <td>Last Name:<input type="text"></td>
    </tr>
    <tr>
        <td><input type="submit" value="Submit"></td>
    </tr>
</table>
vrajesh
  • 2,935
  • 3
  • 25
  • 40
  • http://stackoverflow.com/questions/16157763/javascript-disable-submit-button-until-3-fields-are-not-empty – vrajesh Mar 03 '15 at 06:13

2 Answers2

0
$(document).ready(function(){
 $('input[type="submit"]').attr('disabled','disabled');
 $('input[type="text"]').change(function(){
        if($(this).val != ''){
           $('input[type="submit"]').removeAttr('disabled');
        }
 });
 });

Try this you need to use jQuery selector

nifCody
  • 2,394
  • 3
  • 34
  • 54
0

I have modified your html a little by disabling the button in the first place and then I used jQuery in order to check if all inputs are not empty when one of them changes.

function enableSubmit() {
    var disable = false;
    $("#table input[type='text']").each(function() {
        if ($(this).val()==="") {
            disable = true;
            return false;
        }
    });
    $("#table input[type='submit']").prop("disabled", disable);
}

$("#table").on("change", "input", function() {
    enableSubmit();
});
<html>
<head>
</head>

<body>

    <table id="table">
        <tr>
            <td>First Name: <input type="text"></td>
        </tr>
        <tr>
            <td>Middle Name: <input type="text"></td>
        </tr>
        <tr>
            <td>Last Name:<input type="text"></td>
        </tr>
        <tr>
            <td><input type="submit" value="Submit" disabled></td>
        </tr>
    </table>

</body>

</html>
Timur Osadchiy
  • 5,699
  • 2
  • 26
  • 28