How to save parent id through javascript in nested sublists
<p><strong>I have a nested list  with any number of li's and any number of ul's inside the li's where every li has 2 hidden inputs: an id and an id of the parent.</strong></p>

<pre><code>&lt;ul&gt;
    &lt;li&gt;
        &lt;input type="hidden" name="question[id][]" value="'. $sub['id'] .'" /&gt;
        &lt;input type="hidden" name="question[parent][]" value="'. $question['id'] .'" /&gt;
        &lt;ul&gt;
            &lt;li&gt;
            &lt;input type="hidden" name="question[id][]" value="'. $sub['id'] .'" /&gt;
            &lt;input type="hidden" name="question[parent][]" value="'. $question['id'] .'" /&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;input type="hidden" name="question[id][]" value="'. $sub['id'] .'" /&gt;
        &lt;input type="hidden" name="question[parent][]" value="'. $question['id'] .'" /&gt;
        &lt;ul&gt;
            &lt;li&gt;
            &lt;input type="hidden" name="question[id][]" value="'. $sub['id'] .'" /&gt;
            &lt;input type="hidden" name="question[parent][]" value="'. $question['id'] .'" /&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
        &lt;ul&gt;
            &lt;li&gt;
            &lt;input type="hidden" name="question[id][]" value="'. $sub['id'] .'" /&gt;
            &lt;input type="hidden" name="question[parent][]" value="'. $question['id'] .'" /&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
    &lt;/li&gt;

&lt;/ul&gt;
</code></pre>

<p>I am using jQuery sortable to sort the nested list and now after sorting them I need to save the appropriate parent id to each item in the sublist. </p>

<p>Theoretically the parent id of each sub question is the value of the first input of the second closest ul. 
Also if there is no parent ul, then I want to set the parent id to 0.
How do I find it? This is what I have so far:</p>

<pre><code>$('#reorder_form').submit(function(){
    $('li').each(function() {
        $id = $(this).closest('ul').closest('li').find("input[name='question[id]']").val();
        if(typeof($id) == 'undefined') $id=0;
        $(this).find("input[name='question[parent]']").val($id);
    });
});
</code></pre>

<p>I am not 100% sure that this is how you properly loop through each input because I am hitting each twice. Perhaps I should loop through the li's instead?</p>
