How can I write...
If a user is typing in input1
then do stuff in this table
else if
a user is typing in input2 then do stuff in the other table
I'm new to javascript but I'm learning. The problem I'm having is finding the correct way to write the if
and else
conditions. What I have now doesn't work correctly but it helps illustrate my goal.
<div class = "search">
<input type="text" id = "inputTRUE" placeholder="Search Bought">
<input type="text" id = "inputFALSE" placeholder="Search In Use">
</div>
JavaScript
<script type="text/javascript">
var lis = $(".cartInfosFALSE tr");
var liss = $(".cartInfoTRUE tr");
var list = $.makeArray(lis.map(function(k, v) {
return $(v).text().toLowerCase();
}));
if (liss) {
$("#inputTRUE").keyup(function() {
var userInput = $(this).val();
lis.each(function(index, value) {
$(value).toggle(list[index].indexOf(userInput) >= 0);
});
});
} else {
$("#inputFALSE").keyup(function() {
var userInput = $(this).val();
lis.each(function(index, value) {
$(value).toggle(list[index].indexOf(userInput) >= 0);
});
});
}
</script>
EDIT: -Got rid of if/else but now a new problem.
<script type="text/javascript">
var lis = $(".cartInfosFALSE tr");
var liss = $(".cartInfoTRUE tr");
var list = $.makeArray(lis.map(function(k, v) {
return $(v).text().toLowerCase();
}));
$("#inputTRUE").keyup(function() {
var userInput = $(this).val();
liss.each(function(index, value) {
$(value).toggle(list[index].indexOf(userInput) >= 0);
});
});
$("#inputFALSE").keyup(function() {
var userInput = $(this).val();
lis.each(function(index, value) {
$(value).toggle(list[index].indexOf(userInput) >= 0);
});
});
</script>
The first keyup does nothing and the second one works correctly.