JQuery ul set class dynamically
<p>I'm new to JQuery and struggling to set an attribute of an element inside a list. I have the following html </p>

<pre><code>&lt;ul class="collections"&gt;
&lt;li&gt;&lt;a href="#" id="2"&gt;LINEN &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" id="3"&gt;ANIMAL PRINTS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" id="4"&gt;BASKET WEAVE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" id="5"&gt;FAUX LEATHER&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" id="6"&gt;FAUX SUEDE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" id="7"&gt;HALLMARK&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" id="8"&gt;JUMBO CORD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" id="9"&gt;PHYTHON&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" id="10"&gt;CHENILLE CARLTON&lt;/a&gt;&lt;/li&gt;
</code></pre>

<p></p>

<p>I want to add the attribute class = "current" to the item in the list where the id is 2. </p>

<p>I can set all items using <code>$('.collections a').attr('class','current');</code></p>

<p>I can detect the item with the id of 2 using:</p>

<pre><code>if($('.collections a').attr('id') == 2){
        console.log('WE HAVE A MATCH');

    }
</code></pre>

<p>But here I am unsure how to get a reference to the actual element. </p>

<p>I've managed set the class using the equals method:</p>

<pre><code>        $(".collections a:eq(0)").attr('class','current');
</code></pre>

<p>But would prefer to be able to set it using the id of the element in case the ordering of the list should change. </p>

<p>Any help would be greatly appreciated.</p>

<p>Thanks in advance.</p>

<p>Regards</p>
