button is disable even through the input got value till i have to manually remove and add in the the button become enabled ?
I got this javascript script where i am am able to disable my button however when the data put the input field some value the button is still disabled despite being that that some value in it till i have to manually click on the input field to edit
This is what i mean button is disabled still when i type in my password field

How can i able to disable the button still if value is none but button enable when there value in it?
$('input[type="email"]' && 'input[type="text"]' && 'input[type="password"]' && 'input[id="confirm_password"]' && 'input[id="validationName"]').keyup(function() {
if ($(this).val() != '') {
$(':button[type="submit"]').prop('disabled', false);
} else {
$(':button[type="submit"]').prop('disabled', true);
}
});
CodePudding user response:
You could filter
Alternatively give all fields a required attribute, then the form cannot be submitted
const $fields = $('input[type="email"], input[type="text"], input[type="password"], input[id="confirm_password"], input[id="validationName"]')
const dis = function() {
const empty = $fields.filter(function() {
return this.value === ''
}).length > 0;
$(':button[type="submit"]').prop('disabled', empty)
};
$fields.keyup(dis)
dis()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<form id="myForm">
<input type="email" value="@" />
<input type="text" />
<button type="submit">Submit</button>
</form>
CodePudding user response:
If you are using jQuery, I believe you need to use comma separator , instead of &&.
You could also optimize the if...else by directly assigning the boolean value to disabled prop.
$('input[type="email"], input[type="text"], input[type="password"], input[id="confirm_password"], input[id="validationName"]').keyup(function() {
$(':button[type="submit"]').prop('disabled', $(this).val() === '');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<input type="email" value="@"/>
<input type="text" />
<input type="submit" />
