dimanche 1 novembre 2020

can I do an if else in a Javascript forEach loop?

Learner here! I'm trying to create a (name) search for an html table with a list of people. I've given each row in the table body an id of row-body and a data-attribute of data-name. For each row, I want to check if the search text can be found in the name, and if so, the row should show. Otherwise, the row should hide. Here's my code. How can I implement the if/else?

    $("#search-box").on('keyup', function () {

        const search = $("#search-box").val();

        $("#body-row").forEach($("#body-row"), if (CheckMatch($(this).data("name"), search)) {
            $(this).show();
        }
        else {
            $(this).hide();
        })

    function CheckMatch(n, s) {
        const name = n.toLowerCase();
        const search = s.toLowerCase();
        return name.includes(search);
    }

Aucun commentaire:

Enregistrer un commentaire