I am trying to check if a div exist or not for the purpose that someone might get the result or not.
If the user get the result then class display_box will exist.
So I am using .length to check the class display_box exist or not; however, I have test that it always return 0 no matter there was a class or not.
I try to use ID.
I tried to use $myDiv.html().length
I tried .text(), .content().
I had tried .is(:empty) too. Is that because ajax, so basically, it will consider the class is not existed at all?
ajax_search_load();//this is function to load my ajax
var $myDiv = $('.display_box');
if ( $myDiv.length==0){
alert('fsdfa');
}
As @Vohuman said, if you are making an asynchronous call, then execute your function in the callback of your ajax request, example:
$.ajax({
url : 'foo.bar',
type: 'GET',
success : function (data) {
//show your data
$('#some-container').html(data);
//find your div
var $myDiv = $('#some-container').find('.display_box');
//do some stuff
if ( $myDiv.length==0){
alert('fsdfa');
}
}
})
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments