Reputation: 7688
Is there anyway of checking that an block element remains empty after a click event? Because I need to add some data if the li
remains empty after the click
$('.order_list li').click(function() {
$('#search_block').remove();
if( ! $('#search_block').exists()) {
$(this).html(search_box);
...
exists
its a custom function that I found on stackoverflow as answer
Because of $(this).html(search_box);
the li's content is replaced with an input box, but I want to set another text after the user clicks on another li, without having selected nothing before.
.exists()
is not the problem, the problem is when user click another li and the li above remains empty
$.fn.exists = function () {
return this.length !== 0;
}
The problem http://jsfiddle.net/vsJ96/1/
Upvotes: 1
Views: 710
Reputation: 87073
I think
$('#search_block').length
is easier.
but If you want to know that $('#search_block')
has some content or not then
$('#search_block').contents().length
Upvotes: 1
Reputation: 29568
$('body').on( 'click', 'body:has( #search_block ) .order_list li', function() {
$('#search_block').remove();
$(this).html(search_box);
});
Upvotes: 0
Reputation: 17815
If you are looking to see if an element exists, you only need to check the length property:
if ( $('#search_block').length ) {
}
If you are looking to see if anything exists inside of an element (e.g. for children elements or text nodes), you can check the length of the content:
if ( $('#search_block').contents().length ) {
}
Upvotes: 1