views:

68

answers:

2

The html:

<div id="videos">
    <a href="javascript:loadAndPlayVideo('QXoIMTjk9Xg')"><img src="http://i.ytimg.com/vi/QXoIMTjk9Xg/default.jpg" id="thumb_QXoIMTjk9Xg"></a>
    <a href="javascript:loadAndPlayVideo('nWubfMkDfVs')"><img src="http://i.ytimg.com/vi/nWubfMkDfVs/default.jpg" id="thumb_nWubfMkDfVs"></a>
    <a href="javascript:loadAndPlayVideo('S3WDKXq36WI')"><img src="http://i.ytimg.com/vi/S3WDKXq36WI/default.jpg" id="thumb_S3WDKXq36WI"></a>
    <a href="javascript:loadAndPlayVideo('sGrfLQVVI8A')"><img src="http://i.ytimg.com/vi/sGrfLQVVI8A/default.jpg" id="thumb_sGrfLQVVI8A"></a>
</div>

The problem: We have the id 'nWubfMkDfVs' and we need to know the next ID in order to get playlist functionality.

*Note: it's without the thumb_, the thumb_ is because I use the id nWubfMkDfVs in another div and want to avoid double Id's*

Question 1: How to make a selector that gives us S3WDKXq36WI as response when using nWubfMkDfVs?

Question 2: How to make a selector that gives us QXoIMTjk9Xg as response when using sGrfLQVVI8A?

I tried a lot of selectors but they all failed, I'm very curious in how you will solve this problem!

+1  A: 

Because you are referencing an ID nested inside an a, you would have to do this:

$("#nWubfMkDfVs").parent().next("a").children('img')

Note, first it selects the parent, then the next anchor, then the img child.

Richard June
Great! That's a clear and good answer, thanks! Problem 1 is solved. Problem 2 is still open (having the last id in the list and get the first id in the list as response)
MeProtozoan
+1  A: 

Try this (demo):

HTML

<div id="videos">
    <a href="javascript:loadAndPlayVideo('QXoIMTjk9Xg')"><img src="http://i.ytimg.com/vi/QXoIMTjk9Xg/default.jpg" id="thumb_QXoIMTjk9Xg"></a>
    <a href="javascript:loadAndPlayVideo('nWubfMkDfVs')"><img src="http://i.ytimg.com/vi/nWubfMkDfVs/default.jpg" id="thumb_nWubfMkDfVs"></a>
    <a href="javascript:loadAndPlayVideo('S3WDKXq36WI')"><img src="http://i.ytimg.com/vi/S3WDKXq36WI/default.jpg" id="thumb_S3WDKXq36WI"></a>
    <a href="javascript:loadAndPlayVideo('sGrfLQVVI8A')"><img src="http://i.ytimg.com/vi/sGrfLQVVI8A/default.jpg" id="thumb_sGrfLQVVI8A"></a>
</div>
<br>
current ID: <span class="current"></span>
<br>
<button>Next</button>

Script

$(document).ready(function() {
    var currentID = 'QXoIMTjk9Xg';
    $('.current').html(currentID);

    $('button').click(function() {
        currentID = getNextId(currentID);
        $('.current').html(currentID);
    });

    getNextId = function(id) {
        // default to first video if next isn't found
        var nxt = $('#videos').find('img[id$=' + id + ']').parent().next().find('img').attr('id') || $('#videos img:eq(0)').attr('id');
        return nxt.replace(/thumb_/, '');
    }
})
fudgey
Wow! Great solution! Thnx :)
MeProtozoan