I have table list with many inputs and selects in filter option. The goal to check if any of this fields has content (such as input is not empty, and select has options selected). I can't understand how to make it work with more then one of these element, because now if two inputs has values and one input to clear it sets button to disabled in spite that one of inputs has value. Help me to handle it.
function buttonStatus() {
var buttonDisable = function(){
$('input[type="submit"]').attr('disabled', true)
},
buttonEnable = function(){
$('input[type="submit"]').attr('disabled', false);
}
$('input').on('keyup change', function(){
var inputText = $.trim($('input').val())
if(inputText.length > 0 || $('select option').is(':selected') && $('select option:selected').val() != 0) {
buttonEnable()
}
else {
buttonDisable()
}
})
$('select').on('keyup change', function(){
if ($('option').is(':selected') && $('option:selected').val() != 0 || $.trim($('input').val()).length > 0) {
buttonEnable()
}
else {
buttonDisable()
}
})
}
buttonStatus()
* {
box-sizing: border-box;
}
div {
margin: 50px;
}
input[type="submit"] {
background: green;
border: none;
padding: 5px;
color: #fff;
}
input[type="submit"][disabled] {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="text" />
<input type="text" />
<select>
<option value="0"></option>
<option value="1">Nigeria</option>
<option value="2">Kenya</option>
<option value="3">Tanzania</option>
<option value="4">Uganda</option>
<option value="5">Ethiopia</option>
<option value="6">Mozambique</option>
</select>
<select>
<option value="0"></option>
<option value="1">Nigeria</option>
<option value="2">Kenya</option>
<option value="3">Tanzania</option>
<option value="4">Uganda</option>
<option value="5">Ethiopia</option>
<option value="6">Mozambique</option>
</select>
<input type="submit" disabled>
</div>
$(this)to know what element are you targeting. You can make this:var inputText = $.trim($(this).val()). The problem is in the$('select')because$(this)will be the select, not the input. It's easiest if you put a classname or an id (both different) in the input tags.