0

Technically, this isn't for a 'list' (ul -> li), but for a series of div blocks inside a container div. I have tried many different ways to try and achieve this, but none of them work. What happens is that when I get to the last element, and then click on my 'next button' it takes me to the SECOND element in the list, never the first element. This is generally the code I'm using:

<div class="wrapperClass">
<div class="myClass" id="ID1">
    <div class="anotherClass">
        <input value="myValue1"/>
    </div>
</div>
<div class="myClass" id="ID2">
    <div class="anotherClass">
        <input value="myValue2"/>
    </div>
</div>
<div class="myClass" id="ID3">
    <div class="anotherClass"">
        <input value="myValue3"/>
    </div>
</div>
...
</div>

and

var nextID = $('div#'+ID).next().find('input').val();
var lastID = $('div#'+ID+':last').find('input').val();

if( nextID == lastID )
{
    nextID = $('.wrapperClass').find('input:first').val();
}
// other code
Jonas
  • 121,568
  • 97
  • 310
  • 388
Ajay Mohite
  • 119
  • 3
  • 13
  • The longest title I've ever seen! – honyovk Jul 10 '12 at 01:12
  • `$('div#'+ID+':last')` doesn't make sense ids are unique and there should be no last element. you can try `$('.myClass:last')` instead. – Ram Jul 10 '12 at 01:13
  • I agree! I just edited the code, I forgot to add the 'wrapper' div. My jquery function receives a parameter called 'ID'. I've tried lot's of different things, such as using `$('.myClass:first').find('input').val(), etc. It always takes me back to the 2nd element. – Ajay Mohite Jul 10 '12 at 01:16
  • 1
    the posted code works here: http://jsfiddle.net/RNrg2/ – Ram Jul 10 '12 at 01:19
  • Could you show that JS in context, perhaps in a [fiddle](http://jsfiddle.net)? How is `ID` set? I wouldn't be trying to select by id at all, e.g., if `this` is a particular input I'd be saying `$(this).closest('div.myClass').next().find('input')` (or similar). – nnnnnn Jul 10 '12 at 01:19
  • 1
    Same question `:)` < might be > or < lil difference > http://stackoverflow.com/questions/11386628/how-can-i-use-jquery-to-get-the-value-of-a-child-of-a-child-that-is-in-an-adjace – Tats_innit Jul 10 '12 at 01:22
  • What are you actually trying to do though? – nbrooks Jul 10 '12 at 01:23

1 Answers1

0

I realize my mistake(s). I should have written

if( currentID == lastID )
{
    // blah
}

It was also incorrect for me to write

$('div#'+ID+':last')

Since id's must be unique. Thanks 'undefined' (unnamed person who pointed that out). I'm clearly a newbie.

Ajay Mohite
  • 119
  • 3
  • 13