Jquery each() fallback if no result found
<p>I'm trying to create a JSON object on form submit to pass to Perl.
I have several nested <code>div</code>s with <code>ul</code>s an <code>li</code>s to allow a jQuery-ui sortable sorting.</p>

<p>To be able to save the sorted <code>li</code>s (which spread over several <code>div</code>s) I wrote the following code:</p>

<pre><code>jQuery("form[name='editorview']").submit(function(event) {
    event.preventDefault();
    var values = '{ ';
    jQuery(this).find(".div_abcd").each(function(){
        values += '"'+this.id+'": [';
        jQuery(this).find("div[id^='CONT']").each(function(){
            values += '{"'+this.id+'": [';
            jQuery(this).find("li").each(function(){
                if (jQuery(this).not("class='placeholder'")){
                    values += '"'+this.id+'",';
                } else {
                    values +='"placeholder",';
                }
            });
            values = values.slice(0, -1); //removing trailing ,
            values +=']},';
        });
        values = values.slice(0, -1); //removing trailing ,
        values += '],';
    });
    values = values.slice(0, -1); //removing trailing ,
    values += '}';
    jQuery("#order").val(values);
    document.editorview.submit();
});
</code></pre>

<p>There is a maximum of 4 <code>div</code>s with the class <code>div_abcd</code> but they don't necessarily include other <code>div</code>s. In this case I have the problem that the second  <code>values = values.slice(0, -1);</code> removes the opening <code>[</code> from line 5. </p>

<p>Is there any way to have an <code>else</code> if <code>jQuery(this).find("div[id^='CONT']").each(function(){</code> doesn't match anything. I tried to use <code>.not()</code> but it did not work.</p>

<p><strong>Edit:</strong></p>

<p>Even if my problem is solved, I would still like to know if there is a possibility to know when the each() does not run through even once.</p>

<p><strong>2nd Edit:</strong></p>

<p>Everything clear now. Thanks.</p>
