enrique
enrique

Reputation: 144

Enable button after input text field generated by jquery is filled

Adding of text field are generated by jquery. I just want to enable the submit button after the input text field are typed in. Thanks. Here's the code: https://jsfiddle.net/akoni/kL8jdpdc/

I tried this code, but no luck.

(function() {
$('form > input').keyup(function() {

    var empty = false;
    $('body').find('input[type="text"]').each(function() {
        if (($(this).val() == '')) {
            empty = true;
        }
    });

    if (empty) {
        $('#submit').attr('disabled', 'disabled');
    } else {
        $('#submit').removeAttr('disabled');
    }
});
})()

Upvotes: 1

Views: 1879

Answers (4)

Berkay Yaylacı
Berkay Yaylacı

Reputation: 4513

The issue that you actually faced explained here, basically you should use on() instead of keyup(). And

input[type="text"]

will return less count then form > input, here is the changes

$(document).on("keyup", "input[type='text']", function() {
  var empty = false;
  $('input[type="text"]').each(function() {
    if (($(this).val() == '')) {
      empty = true;
    }
  });

  if (empty) {
    $('#submit').attr('disabled', 'disabled');
  } else {
    $('#submit').removeAttr('disabled');
  }
}); 

jsfiddle result

Hope helps,

Upvotes: 2

Taufik Nur Rahmanda
Taufik Nur Rahmanda

Reputation: 1969

You must use delegation binding for any html elements that is dynamically added.

Try change $('form > input').keyup(function() {

to $('form').on('keyup','input',function() {

Good luck!

Upvotes: 1

Shiladitya
Shiladitya

Reputation: 12181

Here you go with a solution https://jsfiddle.net/kL8jdpdc/9/

(function() {
    $('form').on('keyup', 'input[type="text"]', function() {
				
        var empty = false;
        $('input[type="text"]').each(function() {
            if (($(this).val() == '')) {
                empty = true;
            }
        });

        if (empty) {
            $('#submit').attr('disabled', 'disabled');
        } else {
            $('#submit').removeAttr('disabled');
        }
    });
})()


var i=1;
$(".addmore").on('click',function(){
  $('#submit').attr('disabled', 'disabled');
	count=$('table tr').length;
    var data="<tr><td><input type='checkbox' class='case'/></td>";
    data +="<td><input type='hidden' id='process_id"+i+"' name='process_name"+i+"'/>P"+i+"</td><td><input type='text' id='burst_id"+i+"' class='text-input-grey' name='burst_name"+i+"'/></td></tr>";
	$('table').append(data);
	i++;
});

function select_all() {
	$('input[class=case]:checkbox').each(function(){ 
		if($('input[class=check_all]:checkbox:checked').length == 0){ 
			$(this).prop("checked", false); 
		} else {
			$(this).prop("checked", true); 
		} 
	});
}


$(".delete").on('click', function() {
	$('.case:checkbox:checked').parents("tr").remove();
    $('.check_all').prop("checked", false); 


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method='post' action='' class='subscription-table'>

<table width="500" align='center' border="1" cellpadding="10" cellspacing="5">
  <tr>
    <th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
    <th>Process</th>
    <th>Burst Time</th>
  </tr>
</table>

<button type="button" class='delete'>- Delete</button>
<button type="button" class='addmore'>+ Add Process</button>

											<input id="submit" type="submit" name="submit" disabled="disabled"/>
</form>

Use $('form').on('keyup', 'input[type="text"]' instead of $('form > input'), if you select only input then it will select checkboxes as well.

When you add a new row, you need to disable the submit button as well $('#submit').attr('disabled', 'disabled'); added to the click event of .addmore button.

Hope this will help you.

Upvotes: 0

Mona Lisa
Mona Lisa

Reputation: 193

This should right about do it:

(function() {
$("form > input").keyup(function() {
    var text = $("form > input").val();

    if (text == "") {
        $('#submit').attr('disabled', 'disabled');
    } else {
        $('#submit').removeAttr('disabled');
    }
});
})()

Upvotes: 0

Related Questions