0

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.

3
  • 2
    You don't need if/else for this. Just attach different keyup handlers to each input. They will each fire only when the user is typing into that input. Commented Apr 27, 2016 at 16:38
  • Both your keyup handlers are doing stuff in the same table, not in different tables like your question says. Commented Apr 27, 2016 at 16:40
  • Thanks for responding, quick question. Is the keyup handler $("#inputFALSE") (as an example)? Commented Apr 27, 2016 at 16:42

2 Answers 2

1

Just remove your if/else statement. This way an event listener is attached to each input and handled independently.

$("#inputTRUE").keyup(function() {
    // do stuff
});

$("#inputFALSE").keyup(function() {
    // do other stuff
});

jsfiddle example.

Sign up to request clarification or add additional context in comments.

4 Comments

I did this but still it doesn't work correctly. The first keyup does nothing and the second one works though.
I'm not able to get a fiddle working at all. I'll keep trying though
What browser are you trying it in? Works for me in Chrome, Firefox, Internet Explorer, Edge, Safari & Opera.
it's because I was missing a letter....Working great now though. Thanks for your time!
1

You don't need if / else, beter to use event listeners:

$('#inputTRUE').on('input', function () {
    // do something
});

$('#inputFALSE').on('input', function () {
    // do something else
});

little example here: https://jsfiddle.net/yk0sseL7/

(about using keyup and input events read here - jQuery 'input' event)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.