I have a search page like
<div class="">
<input id="search-input" type="text" class="input-medium search-query span4">
</div>
<div id="result">
</div>
<div id="result-template">
<div class="hide-item item" id="item">
<div style="float:left">
<img class="thumbnail" src="" alt="" />
</div>
<div id="name">
<p class="title"></p>
<p class="views"></p>
</div>
</div>
</div>
and jQuery as
$(function(){
$('#search-input').live('keyup',function() {
var search_input = $(this).val();
var keyword = encodeURIComponent(search_input);
var yt_url = 'http://gdata.youtube.com/feeds/api/videos?q='+keyword+'&format=5&max-results=10&v=2&alt=jsonc';
$.ajax({
url: yt_url,
type: 'GET',
dataType: 'jsonp',
complete: function(xhr, textStatus) {
//called when complete
},
success: function(response, textStatus, xhr) {
if(response.data.items) {
var template = $('#item').clone();
$('#result').html(template);
$.each(response.data.items, function(i, data) {
console.log(data)
search_data = {
'id': data.id,
'title': data.title,
'views': data.viewCount,
'thumbnail': data.thumbnail['sqDefault'],
}
video_result_template(search_data);
});
} else {
// var template = $('#item').clone();
// $('#result').html(template);
}
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});
});
});
// filling out the search template
function video_result_template(data) {
var item = $('#item').clone();
item.removeClass('hide-item');
item.find('img').attr('src', data.thumbnail);
item.find('.title').html(data.title);
item.find('.views').html(data.views);
$('#item').attr('id', data.id);
item.addClass('view-item');
// $('#result').append(item).fadeIn(); // slow/fast?
$('#result').append(item).fadeIn('slow');
}
I get the results on the div with title, views, url. Now when I click div, the following jQuery is fired
$(function(){
$('.item').live('click', function(){
// alert(this.id);
console.log($(this));
var url = $('#video-frame').attr('src');
var new_url = url.replace(/embed\/[\w -]*/g, 'embed/' + this.id);
alert($('.title').html() + ',' + new_url);
$('#video-frame').attr('src', new_url);
});
});
which alerts right title but the url(or video_id is of the next video in the list).
When I see my html from firefox I see the following
<div id="result" style="display: block;">
<div id="lqT_dPApj9U" class="item view-item">
<div style="float: left;"><img alt="" src=
"http://i.ytimg.com/vi/S2nBBMbjS8w/default.jpg" class="thumbnail" /></div>
<div id="name">
<p class="title">Coke 2012 Commercial: "Catch" starring NE_Bear</p>
<p class="views">619167</p>
</div>
</div>
<div id="hKoB0MHVBvM" class="item view-item">
<div style="float: left;"><img alt="" src=
"http://i.ytimg.com/vi/lqT_dPApj9U/default.jpg" class="thumbnail" /></div>
<div id="name">
<p class="title">Coca-Cola Happiness Machine</p>
<p class="views">4791156</p>
</div>
</div>
<div id="item" class="item view-item">
<div style="float: left;"><img alt="" src=
"http://i.ytimg.com/vi/S2nBBMbjS8w/default.jpg" class="thumbnail" /></div>
<div id="name">
<p class="title">Coke 2012 Commercial: "Catch" starring NE_Bear</p>
<p class="views">619167</p>
</div>
</div>
</div>
- If seen clearly the first and last div has same data elements
- infact the difference is that the last div is copy of first element
- The last element has invalid id=”item”(must be video id)
Question
How can I fix this issue?
JSFiddle
http://jsfiddle.net/hhimanshu/F5NHK/
Required
After search, click on any video div and it must give right video id and title
P.S. in fiddle clicking on list is not doing anything(don’t know why, it works on my system though)
In function
replace
$('#item').attr('id', data.id);toitem.attr('id', data.id);Thank you @arhen for providing this solution