-1

I have a main dive and inside that there are 2 dives. I need to check that if div's data is empty don't show them. BTW data comes from API.

Here is code:

<div id ="select">
     <ul>
         <li><div id="product1" style="font-family:arial; font-size:13px; border-bottom:1px solid #b8b8b8; background-color:#eeeeee; padding:10px;">

               <strong>{$feed/API Feed_142197/__data["1-Title"]}</strong>
               <br/>
               <p>{$feed/API Feed_142197/__data["1-Desc"]}</p>

              <div style="display:inline; align:left; color:green; font-weight:bold;">{$feed/API Feed_142197/__data["1-Price"]}</div>

               <div style="display:inline; padding-left:80px; padding-right:5px;">click to call image</div></div>
         </li>


         <li><div id="product2" style="font-family:arial; font-size:13px; border-bottom:1px solid #b8b8b8; background-color:#eeeeee; padding:10px;">
              <strong>{$feed/API Feed_142197/__data["2-Title"]}</strong>
              <br/>
              <p>{$feed/API Feed_142197/__data["2-Desc"]}</p>
              <div style="display:inline; align:left; color:green; font-weight:bold;">{$feed/API Feed_142197/__data["2-Price"]}</div>
              <div style="display:inline; padding-left:80px; padding-right:5px;">click to call image</div></div>
         </li>

</div>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Ellie
  • 11
  • 2
  • As concerned as your code I can see the 2 div situation twice. Do you mean the two divs without id's? – Reporter May 11 '12 at 09:05
  • You can find the solution to your problem by reading [How to check if element has any children in Javascript?](http://stackoverflow.com/q/2161634/218196) and [javascript hide/show element](http://stackoverflow.com/q/6242976/218196). – Felix Kling May 11 '12 at 09:09

2 Answers2

0

using jQuery

var div1 = $('#product1'),
    div2 = $('#product2');

if (!(div1.text() || div2.text())) {
     $('#select').hide()
}
Guard
  • 6,816
  • 4
  • 38
  • 58
0

Here is a modern browser, no libraries required solution.

var nodes = document.querySelectorAll('#select div:empty');
for (var i = 0, il = nodes.length; i < il; i++) {
    nodes[i].style.display = 'none';
}
Moncader
  • 3,388
  • 3
  • 22
  • 28