title: Using conditions in jquery append function
body: <p>Look at the code block below. Its working for me, but I need to write it with conditions.
  Condition mean, I need to add each <code>span</code> elements if <code>item</code> values is not empty.</p>
  <pre><code>create: function () {
    $(this).data('ui-autocomplete')._renderItem = function (ul, item) {
    return $('&lt;li class=&quot;dropdown-item&quot;&gt;')
      .append('&lt;div class=&quot;dropdown-item-wrapper&quot;&gt;'
             +' &lt;span&gt;&lt;span&gt;'+item.item_code+'&lt;/span&gt;&lt;/span&gt;'
             +' &lt;span&gt;&lt;span&gt;'+item.brand+'&lt;/span&gt;&lt;/span&gt;'
             +' &lt;span&gt;&lt;span&gt;'+item.stock+''+item.unit+'&lt;/span&gt;&lt;/span&gt;'
             +' &lt;span&gt;&lt;span&gt;'+item.item_name+'&lt;/span&gt;&lt;/span&gt;'
             +'&lt;/div&gt;')                   
      .append('&lt;/li&gt;')
      .appendTo(ul); 
  }
  </code></pre>
  <p>So, I tried it in this way, But I coudn't figure it out.</p>
  <pre><code>create: function () {
    $(this).data('ui-autocomplete')._renderItem = function (ul, item) {
  
    var $li  = $('&lt;li class=&quot;dropdown-item&quot;&gt;');
    var $div = $('&lt;div class=&quot;dropdown-item-wrapper&quot;&gt;');
    if (item.item_name) {
      $('&lt;span&gt;&lt;span&gt;', {text: item.item_name}).appendTo($div); 
    }
  
    $div.appendTo($li);
    return $li.appendTo(ul); 
  } 
  </code></pre>