1

I'd like to pass a function the id of an <LI> and have it return the id of the next one available within the <UL>.

The id's may not be in order, and there are other elements nested within the <LI> so I can't just get the next one of those.

I've searched all over the web and tried various combinations without success.

HTML:

<ul class="timeline" id="timeline">

    <li id="timelineLI-1A4">
        <div class="timelineIcon letter"></div>
        <div class="dateContainer"></div>
    </li>

    <li id="timelineLI-2">
        <div class="timelineIcon mail"></div>
        <div class="dateContainer"></div>
    </li>

</ul>

JavaScript/JQuery Code:

function openNextTile() {
    console.log(lastTileOpened);

    // Get the current tile that's open
    if (lastTileOpened == null) {
        openFirstTile();
    } else {
        console.log('opening next tile with keyboard');
        var getNextLIID = $('ul#' + 'timelineTile-' + lastTileOpened).next();
        animateTile(getNextLIID);
    }
}
Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
Michael Bellamy
  • 543
  • 1
  • 8
  • 16

2 Answers2

3

This seems very easy to accomplish:

function getNext(id) {
    return $("#" + id).next("li").attr("id");
}

Demo: http://jsfiddle.net/QKRrM/

tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • Thanks - but that returns me 'undefined'? – Michael Bellamy Aug 08 '13 at 16:12
  • So it does, I copy and pasted your code so I must be doing something wrong - I'll check first thing in the morning once I'm back at work. – Michael Bellamy Aug 08 '13 at 17:06
  • 1
    @MichaelBellamy -- No problem. If you're actually passing the jQuery object into the function, then you'll have to tweak the function (replace `$("#" + id)` with whatever param you pass in) – tymeJV Aug 08 '13 at 17:35
0
var nextId = function( id ) {
    return $('#' + id).next().attr('id');
};

Example usage:

<ul>
    <li id="one">...</li>
    <li id="two">...</li>
</ul>

console.log(nextId('one')); // "two"
André Dion
  • 21,269
  • 7
  • 56
  • 60