Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery if data not empty add class issue

not sure why this isn't working still fairly new with js. I am trying to check if there is any data and if there is then it should add a hidden class or should remove it if it has no data.

 $(function() {

    $('#search').keyup(function() {
        var followerList=$("#followerlist");
        if($('#search').val() !== ""){
            followerList.addClass('hidden');
        }else{
            followerList.removeClass('hidden');
        };
        $.ajax({
            type: "POST",
            url: "/search/",
            data: {
                'search_text': $('#search').val(),
                'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()
            },
            success: searchSuccess,
            dataType: 'html'
        });
    });
});



function searchSuccess(data, textStatus, jqXHR)
{
    $('#search-results').html(data);
}
like image 499
Josh_Fokis Avatar asked Dec 05 '25 07:12

Josh_Fokis


1 Answers

Your searchSuccess function has to be like this:

function searchSuccess(data, textStatus, jqXHR)
{
        var followerList=$(".followerlist");
        $('#search-results').html(data);
        if(data !== "")
        {
                followerList.addClass("hidden");
        }
        else
        {
                followerList.removeClass("hidden");
        }
}
like image 175
Rayon Avatar answered Dec 07 '25 19:12

Rayon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!