Using Bootstrap 4.
I have four text inputs, if any of the text inputs contains any values, I would like to add a class to the button.
When the button is pressed, I want to clear the input values ββand remove the class from the button.
I have half the work (pressing a button successfully clears all inputs).
My code is below;
$(document).ready(function() { $("#filterRecords input").on("keyup change", function() { $("#filterRecords input").each(function() { var element = $(this); if (element.val().length > 0) { $('#resetFilter').addClass('btn-outline-primary'); } }); }); $('#resetFilter').click(function() { $('input[type=text]').val('').change(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row filterRecords"> <div class="input-group col-xs-6 col-sm-6 col-md-3 mb-3"> <input type="text" id="searchName" class="form-control" placeholder="Search Name"> </div> <div class="input-group col-sm-6 col-md-3 mb-3"> <input type="text" id="searchLang" class="form-control" placeholder="Search Language"> </div> <div class="input-group col-sm-6 col-md-3 mb-3"> <input type="text" id="yearMin" class="form-control start" placeholder="Search Start Year"> </div> <div class="input-group col-sm-6 col-md-3 mb-3"> <input type="text" id="yearMax" class="form-control end" placeholder="Search End Year"> </div> </div> <div class="row justify-content-md-center"> <div class="col-md-auto"> <button class="btn btn-sm btn-default" type="button" id="resetFilter">Reset Filters</button> <hr> </div> </div>
Link to the script .
Any help would be appreciated.
javascript jquery html twitter-bootstrap
TheOrdinaryGeek
source share