Menu traversing on next and prev button click up and down Can anyone optimize this code?  I am new to jQuery.

I want add class on next and previous button click.  I wrote this code that works for me, but could anyone optimize it using jQuery predefined methods?  That would be helpful. 

$(document).ready(function () {
    var length = $('#slides li').size() - 1;
    var curren = 0;
    console.log(length);
    $('.next').on('click', function () {
        if (curren &gt;= 0 &amp;&amp; curren &lt; length) {
            curren++;
            $('.selected').removeClass('selected');
            $('#slides li:eq(' + curren + ')').addClass('selected');
        }
    });
    $('.prev').on('click', function () {
        if (curren &gt;= 1) {
            curren--;
            $('.selected').removeClass('selected');
            $('#slides li:eq(' + curren + ')').addClass('selected');
        }
    });
});


My HTML code:

&lt;ul id="slides"&gt;
    &lt;li class="selected"&gt;first&lt;/li&gt;
    &lt;li&gt;second&lt;/li&gt;
    &lt;li&gt;third&lt;/li&gt;
    &lt;li&gt;fourth&lt;/li&gt;
    &lt;li&gt;five&lt;/li&gt;
    &lt;li&gt;six&lt;/li&gt;
&lt;/ul&gt;


CSS code:

.selected{
color:red;
}
There's not much in the way of predefined jQuery methods to add. Here's what I came up with though...

$(document).ready(function () {
    var $slides = $('#slides li'); //Save repeated selector result. 
    var length = $slides.length - 1; //size is deprecated
    var curren = 0;
    console.log(length);
    // used .click due to personal preference .on('click', does the same thing
    $('.next').click(function () {
        if (curren &lt; length) { //Removed check that will always be true
            curren++;
            changeSelected(curren); //Moved repeated code to function
        }
    });
    $('.prev').click(function () {
        if (curren &gt;= 1) {
            curren--;
            changeSelected(curren);
        }
    });

    function changeSelected(index) {
        $('.selected').removeClass('selected');
        $slides.eq(index).addClass('selected'); //Use previously selected object
    }
});


Here's a link to the doc for .size() where you can find it is deprecated.