I am building a form with a search input that shows and hides an x
inside the search input to clear the contents of that one input, but not the others in the form. I've written a function to show and hide the x
based on whether or not the search input specifically is empty. Currently, my page renders the form twice based on the page width (I know that's not the best, but it's what I have currently and I'd like to leave it alone). To do that, I need to check if either instance of the search input on the page has content.
The code below works, but I'm concerned that it's fragile. If a form is added or removed from the page, I'd like this code to keep working. Any suggestions on how to clean up the line below?
function toggleFilterInput() {
// The line I'd like to clean up
if ( !$(".filter-input").first().val() && !$(".filter-input").last().val() ) {
$(".filter-clear").addClass("hidden");
} else {
$(".filter-clear").removeClass("hidden");
}
}
EDIT: I did see this possible duplicate, but didn't like any of the answers, thus why I'm asking a similar question again.