Enable/Disable Clear button based on the search criteria
There are 3 text fields in a search form and a clear button in it.
Here is what I want to do -
1 - First the clear button will be disabled
2 - When the text is entered in any one of the text boxes then the Clear
button will be enabled
3 - The Clear button will be disabled only when there is no text in either
of the text boxes.
4 - We can also add more search criteria. That means there can be any
number of text boxes
Here is my code:
var toValidate = $("#basicSearchFields input"),
clearBtn = $("#clearBasicSearch");
toValidate.keyup(function () {
toValidate.each(function () {
if ($(this).val().length > 0) {
valid = true;
} else {
valid = false;
}
(valid === true) ? clearBtn.removeClass('disabled') :
clearBtn.addClass('disabled');
});
});
Actually what this code is doing when text is entered into any of the text
boxes the button is getting enabled but clearing the texts from the text
box is not disabling the button again. Please help me out!!
No comments:
Post a Comment