jquery - How to enable button based on both checkbox and textbox content -
i have following code :
<html> <body> <form action=""> <input type="checkbox" id="condition" value="conditions">conditions<br> <input type="text" id="txtagree" disabled> <input type="submit" name="generate" id="generate" disabled> </form> </body>
and wrote following method in jquery
if ($("#condition").is(":checked")) { $('#txtagree').prop('disabled',false); $('#txtagree').keyup(function () { if ($(this).val().length != 0) { $('#generate').prop('disabled', false); } else { $('#generate').prop('disabled', true); } }) } $('#generate').prop('disabled', false); }
but button getting enabled checking checkbox itself. requirement if check box checked,textbox enabled , if textbox not empty, button should enabled.
a shorter version you:
$('#condition,#txtagree').on('change keyup', function() { $('#txtagree').prop('disabled', !$('#condition:checked').length); $('#generate').prop('disabled', !($('#condition:checked').length && $('#txtagree').val())); });
jsfiddle: https://jsfiddle.net/trueblueaussie/mt4uokvx/
it listens both keyup
, change
events on both controls, enables text input based on checkbox , button based on checkbox , edit control. in theory needs check length of text, require uncheck clear text :)
Comments
Post a Comment