Reputation: 99
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
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