The task is to check if the div is empty? The content in the div is loaded via ajax, as well as a script that, under certain conditions, displays an audible alert. Actually, I can’t figure out how to prevent the script from triggering an alert when there is content in the div. Tried to do through this design:

if($(".grid_call_main_orders_one_content").is(':empty')){ var audio = new Audio(); audio.src = 'alert.mp3'; audio.autoplay = true; setTimeout(function () { alert('Новый заказ!Обновите страницу! '); }, 3000); } 

But the scheme is not working.

    2 answers 2

    It is possible through $('selector').text().length ;

     let a = $('#a').text().length; let b = $('#b').text().length; a > 0 ? alert ('a have value') : alert ('a empty'); b > 0 ? alert ('b have value') : alert ('b empty'); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='a'>hi</div> <div id='b'></div> 

    • According to your algorithm, the next block will also be considered empty <div id='b'><p></p><div><span></span></div></div> . Maybe add a check on the children . - Stepan Kasyanenko

    In jQuery, there is a .html () method - it takes content from an element.
    The principle is this, we take the content and check it for emptiness, for example:
    $ ("here"). html () == "";

     var contentGridBlock = $(".grid_call_main_orders_one_content"); // Кнопки для теста $(".gridFull").click(function() { contentGridBlock.html("Я наполнен"); }); $(".gridClear").click(function() { contentGridBlock.html(""); }); $(".gridCheck").click(function() { // Тут самое интересное var checkContent = contentGridBlock.html(); if(checkContent == "") { alert("Блок пустой"); } else { alert("Блок с контентом"); } }); 
     button { display: block; } 
     <div class="grid_call_main_orders_one_content"></div> <button class="gridCheck">Проверка контента</button> <button class="gridFull">Наполнить блок тестовым контентом</button> <button class="gridClear">Удалить из блока тестовый контент</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>