Identifing if next/previous element exist
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/7678499/jquery-checking-if-next-element-exists">jQuery: Checking if next element exists</a>  </p>
</blockquote>



<p>I'm implementing a custom slider using jquery. There I want to give facility to load next/previous image when user press <code>right</code>/<code>left</code> keys respectively.</p>

<p>It is working fine but when cursor reach <code>last</code>/<code>first</code> element, code breaks as it do not find  further elements. My code is as follow:</p>

<pre><code>function movePointer(dir){
    var selected = $("#imgslider &gt; ul &gt; li &gt; img.siddslideselected");
    console.log(selected);
    var selectedli = selected.parent();
    if(dir=='left'){
        var nabourele = selectedli.prev()
    }else if(dir=='right'){
        var nabourele = selectedli.next();
    }
    console.log(nabourele);
    if(nabourele){
        selected.removeClass('siddslideselected');
        nabourele.children().addClass('siddslideselected').trigger('click');
    }
}
</code></pre>

<p>If current position of cursor is in middle, everything work fine but if cursor is on first element, pressing <code>left</code> key breaks the code. I try to stop that by checking <code>if(nabourele){</code>, but it is not working. console log for <code>nabourele</code> show <code>[]</code> in that case.</p>

<p>How can I test condition if next/prev element exist or not?</p>
