Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if div has content show div

ok heres what i have... it works fine but it looks for a word rather than content. i just want it to show when there is any content..

 $(document).ready(function(){
       if ($("#box3:contains('Product')").length) {
          $('#third').show();
       }                                           
    });

i dont think you need the html for this

it looks for 'Product' how do i make it just look for content >0

<div id="first" class="tab" >
    <div class="tabtxt">
        <a>DETAILS</a>
    </div>
</div>
<div class="tab" id="second">
    <div class="tabtxt">
        <a>INSPIRATION</a>
    </div>
</div>
<div class="tab" id="third" style="display:none">
    <div class="tabtxt">
        <a>NOTES</a>
    </div>
</div>

<div class="boxholder">
    <div style="overflow: hidden; display:block" class="box" id="box1">
        <div style="padding: 10px; line-height:16px">
            %%Panel.ProductDescription%%
        </div>
    </div>
    <div style="overflow: hidden; display:none" class="box" id="box2">
        <div style="padding: 10px; line-height:16px">
            %%Panel.ProductWarranty%%
        </div>
    </div>
    <div style="overflow: hidden; display:none" class="box" id="box3">
        <div style="padding: 10px; line-height:16px">
            %%Panel.UPC%%
        </div>
    </div>
</div>

its an interspire shopping cart so the %%panel.upc%% calls in something inserted through the admin panel. in this case if there is nothing.. it shows as blank space in the code (viewing source in browser).

like image 590
Alex Avatar asked Oct 08 '10 18:10

Alex


People also ask

How do I check if a div is empty?

Use the childNodes property to check if a div element is empty. The childNodes property returns a NodeList of the element's child nodes, including elements, text nodes and comments. If the property returns a value of 0 , then the div is empty.

How do I hide a div?

We hide the divs by adding a CSS class called hidden to the outer div called . text_container . This will trigger CSS to hide the inner div.


3 Answers

If you want to check for text, you can use the text() method:

 $(document).ready(function(){
   if ($("#box3").text().length > 0) {
     $('#third').show();
   }                                           
 });

Or for html:

 $(document).ready(function(){
   if ($("#box3").html().length > 0) {
     $('#third').show();
   }                                           
 });
like image 109
Sarfraz Avatar answered Oct 05 '22 03:10

Sarfraz


For the updated question: Check the trimmed text of the inner <div>, like this:

 if ($.trim($("#box3 div").html())) {
   $('#third').show();
 }  

Previous answer: If you want to show if it has anything, then checking :not() against :empty works:

 if ($("#box3:not(:empty)").length) {
   $('#third').show();
 }  

If you want to check for any elements (not possibly whitespace only), then use :has(*), like this:

 if ($("#box3:has(*)").length) {
   $('#third').show();
 }  
like image 31
Nick Craver Avatar answered Oct 05 '22 03:10

Nick Craver


You can also use the CSS3 selector :empty

div#empty-div:empty {
   display: none;
}

The :empty selector is only targeting just empty elements, without white space.

like image 22
KKumar Avatar answered Oct 05 '22 04:10

KKumar