Youss
Youss

Reputation: 4232

Remove or hide div if undifined/null or empty

I have a script which can display information based up on url and save this information: moskah.nl

The problem is that if you would click the button without giving url than the script will give an empty div. Just reload the page and you will see it will still be there.

What can I do in Jquery in order to remove or hide these emty divs on document ready?

Upvotes: 0

Views: 2040

Answers (4)

Pradip
Pradip

Reputation: 1537

I hope this helps.

You can use .is() function after loading the div.

if( $('#leftmenu').is(':empty') ) {
    // REMOVE

You can also use the length property:

if( $('#leftmenu:empty').length ) {
    // REMOVE

Or you can trim and then check.

if( !$.trim( $('#leftmenu').html() ).length ) {
    // REMOVE

Please let me know if this works. Implement and let me know.

EDITED

USE

$(document).ready(function() { $('div:empty').remove(); }); 

I tried it in Firefox. It worked.

Upvotes: 0

Pranav
Pranav

Reputation: 8871

Suppose your div has classname "item", then you have loop through each div and check which one is empty and hide them.

  $(document).ready(function() {
    $('#feed').children('div').each(function() {
        if(($(this).attr('data-url')=='undefined'))
        $(this).hide();
    });
     //for first div only special check:-
      if(($('.item.first').attr('data-url')=='undefined'))
        $('.item.first').hide();

    });

Upvotes: 0

Hank
Hank

Reputation: 731

Why not do some data validation to stop the form submitting empty data instead of just hiding it? Something like this

$('#id_submit').attr('disabled', 'disabled');

$('#id_submit').click(function(event){
    if ($('#id_status').val().length > 0) {
        $('#id_submit').removeAttr('disabled');
    }
    else
    {
    event.PreventDefault()
    }
});

Upvotes: 0

prashanth
prashanth

Reputation: 2099

You can try $("div:empty").remove(); or $("div:empty").hide();

Since there is no code provided, I am not really sure if this is what you want.

Upvotes: 0

Related Questions