apply one jquery function to multiple selectors?
<p>I have a pretty complex dom-tree with various exceptions etc. So I don't want to use </p>

<p>I wanna do functions like "for every error message I wanna add a class to the previous input".</p>

<p>Right now I'm doing it with tree traversal, but for various reasons it's not a good solution. Is there a good way to directly target the correct input value? I have full control over the html, so I can add classes and such to them if needed!</p>

<p>Actual HTML:</p>

<pre><code>&lt;label for="from"&gt;FrÃ¥n&lt;/label&gt;
    &lt;input value="as" title="Ort, gata eller kommun" name="from" id="from" class="field from input_error" type="text"&gt;
    &lt;div class="spacer"&gt;
        &lt;div class="error visible"&gt;
            &lt;p&gt;Vilken adress menar du? VÃ¤lj i listan!&lt;/p&gt;
            &lt;ul&gt;
                &lt;li&gt;
                    &lt;a href="addtrip?from=American%20Samoa&amp;amp;to=&amp;amp;when=&amp;amp;got_car=1&amp;amp;name=&amp;amp;email=&amp;amp;phone=&amp;amp;details=&amp;amp;posted" class="adress"&gt;American Samoa&lt;/a&gt;
                    &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href="addtrip?from=Assam,%20India&amp;amp;to=&amp;amp;when=&amp;amp;got_car=1&amp;amp;name=&amp;amp;email=&amp;amp;phone=&amp;amp;details=&amp;amp;posted" class="adress"&gt;Assam, India&lt;/a&gt;
                    &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href="addtrip?from=Elias,%20El%20Gaiara,%20Azbakia,%20Cairo,%20Egypt&amp;amp;to=&amp;amp;when=&amp;amp;got_car=1&amp;amp;name=&amp;amp;email=&amp;amp;phone=&amp;amp;details=&amp;amp;posted" class="adress"&gt;Elias, El Gaiara, Azbakia, Cairo, Egypt&lt;/a&gt;
                    &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href="addtrip?from=Amberg,%2092224,%20Germany&amp;amp;to=&amp;amp;when=&amp;amp;got_car=1&amp;amp;name=&amp;amp;email=&amp;amp;phone=&amp;amp;details=&amp;amp;posted" class="adress"&gt;Amberg, 92224, Germany&lt;/a&gt;
                    &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href="addtrip?from=As%20-%20Bras%C3%ADlia,%20Brasilia%20-%20Distrito%20Federal,%20Brazil&amp;amp;to=&amp;amp;when=&amp;amp;got_car=1&amp;amp;name=&amp;amp;email=&amp;amp;phone=&amp;amp;details=&amp;amp;posted" class="adress"&gt;As - BrasÃ­lia, Brasilia - Distrito Federal, Brazil&lt;/a&gt;
                    &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href="addtrip?from=As,%205753%20Deurne,%20The%20Netherlands&amp;amp;to=&amp;amp;when=&amp;amp;got_car=1&amp;amp;name=&amp;amp;email=&amp;amp;phone=&amp;amp;details=&amp;amp;posted" class="adress"&gt;As, 5753 Deurne, The Netherlands&lt;/a&gt;
                    &lt;/li&gt;
                &lt;/ul&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;label for="to"&gt;Till&lt;/label&gt;
    &lt;input value="" title="Ort, gata eller kommun" name="to" id="to" class="field to help input_error" type="text"&gt;
    &lt;div class="spacer"&gt;
        &lt;div class="error"&gt;
            &lt;p&gt;Var ska du?&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;label for="when"&gt;NÃ¤r&lt;/label&gt;
    &lt;div class="jdpicker_w"&gt;
        &lt;input value="" title="YYYY-MM-DD" name="when" id="when" class="field when input_error" type="text"&gt;
        &lt;div class="date_selector"&gt;
            &lt;div class="nav"&gt;
                &lt;div class="error_msg"&gt;&lt;/div&gt;
                &lt;p class="month_nav"&gt;
                    &lt;span class="button prev" title="[Page-Up]"&gt;Â«&lt;/span&gt;
                    &lt;span class="month_name"&gt;November&lt;/span&gt;
                    &lt;span class="button next" title="[Page-Down]"&gt;Â»&lt;/span&gt;
                    &lt;/p&gt;
                &lt;p class="year_nav"&gt;
                    &lt;span class="button prev" title="[Ctrl+Page-Up]"&gt;Â«&lt;/span&gt;
                    &lt;span class="year_name" id="year_name"&gt;2010&lt;/span&gt;
                    &lt;span class="button next" title="[Ctrl+Page-Down]"&gt;Â»&lt;/span&gt;
                    &lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;div class="spacer"&gt;
        &lt;div class="error"&gt;
            &lt;p&gt;NÃ¤r vill du Ã¥ka?&lt;/p&gt;


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

<p></p>
