Josh_Fokis
Josh_Fokis

Reputation: 99

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);
}

Upvotes: 3

Views: 80

Answers (1)

Rayon
Rayon

Reputation: 36609

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");
        }
}

Upvotes: 1

Related Questions