> I have the following on a JQuery Code:
>
>    $('<li class="DynamicList"></li>').appendTo('#Themes')
>    .append(subject)
>    .append('<br />')
>    .append(levels)
>    .append('<br />')
>
> How can I add
>
>    .append(subject)
>    .append('<br />')
>
> only if subject is not empty and
>
>    .append(levels)
>    .append('<br />')
>
> only if levels is not empty?
>
> Levels and Subject are declared as: var subject = [], levels = [];

I'm not sure how you're appending arrays to an element, but in general
you can handle this by stashing a reference to the newly-created <li>
and then conditionally appending in a series of statements.  Something
like:

var subject = 'something', levels = '', $li = $('<li
class="DynamicList"></li>').appendTo('#Themes');
if (subject) {
  $li.append(subject).append('<br />');
}
if (levels) {
  $li.append(levels).append('<br />');
}

Since the initial appendTo call will return a jQuery object that wraps
the newly-created <li>, you can either chain it (as in your example)
or stash it in a variable for later re-use (as in my example).

Reply via email to