Reputation: 1027
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!
Upvotes: 1
Views: 8375
Reputation: 86413
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_/, '');
}
})
Upvotes: 1
Reputation: 706
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.
Upvotes: 2