azarudeen azarudeen - 2 months ago 21
CSS Question

combine the js and html code for youtube autocomplete search box.

I have a code for youtube autocomplete, but i dont know how to combine this code ,please make this code work. this serch should display the youtube autocomplete queries. is it is possible to combine this code

js code

$("#youtube").autocomplete({
source: function(request, response){
/* Google Developer ID (optional) */
var apiKey = 'AI39si7ZLU83bKtKd4MrdzqcjTVI3DK9FvwJR6a4kB_SW_Dbuskit-mEYqskkSsFLxN5DiG1OBzdHzYfW0zXWjxirQKyxJfdkg';
/* Search keyword */
var query = request.term;
/* youtube sorgusu */
$.ajax({
url: "http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q="+query+"&key="+apiKey+"&format=5&alt=json&callback=?",
dataType: 'jsonp',
success: function(data, textStatus, request) {
response( $.map( data[1], function(item) {
return {
label: item[0],
value: item[0]
}
}));
}
});
},
/* You can use transaction is selected here to */
select: function( event, ui ) {
$.youtubeAPI(ui.item.label);
}
});


The pressure on the button Search

$('button#submit').click(function(){
var value = $('input#youtube').val();
$.youtubeAPI(value);
});


Youtube Search Function

$.youtubeAPI = function(kelime){
var sonuc = $('#sonuc');
sonuc.html('Arama gerçeklestiriliyor...');
$.ajax({
type: 'GET',
url: 'http://gdata.youtube.com/feeds/api/videos?q=' + kelime + '&max-results=15&v=2&alt=jsonc',
dataType: 'jsonp',
success: function( veri ){
if( veri.data.items ){
sonuc.empty();
$.each( veri.data.items, function(i, data) {
sonuc.append('<div class="youtube">\
<img src="' + data.thumbnail.sqDefault + '" alt="" />\
<h3><a href="javascript:void(0)" onclick="$.youtubePlay(\'' + data.id + '\', \'' + data.content[5] + '\')">' + data.title + '</a></h3>\
<p>' + data.description + '</p>\
</div>\
<div class="youtubeOynat" id="' + data.id + '"></div>');
});
}
else {
sonuc.html('<div class="hata"><strong>' + kelime + '</strong> ile ilgili hiç video bulunamadi!</div>');
}
}
});
}


YouTube Video Playback Function

$.youtubePlay = function(yid, frame){
$('.youtubeOynat').slideUp().empty();
$('#'+yid).slideDown().html('<iframe src="'+ frame +'&autoplay=1" style="width: 100%; box-sizing: border-box; height: 300px" />');
}


html code

<div class="arama">
<form action="" onsubmit="return false">
<h2>Youtube API jQuery AutoComplete</h2>
<div class="ui-widget">
<label for="youtube">Youtube SEARCH: </label>
<input id="youtube" />
<button id="submit">SEARCH</button>
</div>
</form>
</div>

Answer

Your code works fine. I just tested it. Copy the code below and try it.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#youtube").autocomplete({
    source: function(request, response){
        /* Google Developer ID (optional) */
        var apiKey = 'AI39si7ZLU83bKtKd4MrdzqcjTVI3DK9FvwJR6a4kB_SW_Dbuskit-mEYqskkSsFLxN5DiG1OBzdHzYfW0zXWjxirQKyxJfdkg';
        /* Search keyword */
        var query = request.term;
        /* youtube sorgusu */
        $.ajax({
            url: "http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q="+query+"&key="+apiKey+"&format=5&alt=json&callback=?",  
            dataType: 'jsonp',
            success: function(data, textStatus, request) { 
               response( $.map( data[1], function(item) {
                    return {
                        label: item[0],
                        value: item[0]
                    }
                }));
            }
        });
    },
    /* You can use transaction is selected here to */
    select: function( event, ui ) {
        $.youtubeAPI(ui.item.label);
    }
    });

            $('button#submit').click(function(){
    var value = $('input#youtube').val();
        $.youtubeAPI(value);
    });


            $.youtubeAPI = function(kelime){
    var sonuc = $('#sonuc');
    sonuc.html('Arama gerçeklestiriliyor...');
    $.ajax({
        type: 'GET',
        url: 'http://gdata.youtube.com/feeds/api/videos?q=' + kelime + '&max-results=15&v=2&alt=jsonc',
        dataType: 'jsonp',
        success: function( veri ){
            if( veri.data.items ){
                sonuc.empty();
                $.each( veri.data.items, function(i, data) {
                    sonuc.append('<div class="youtube">\
                        <img src="' + data.thumbnail.sqDefault + '" alt="" />\
                        <h3><a href="javascript:void(0)" onclick="$.youtubePlay(\'' + data.id + '\', \'' + data.content[5] + '\')">' + data.title + '</a></h3>\
                        <p>' + data.description + '</p>\
                    </div>\
                    <div class="youtubeOynat" id="' + data.id + '"></div>');
                });
            }
            else {
                sonuc.html('<div class="hata"><strong>' + kelime + '</strong> ile ilgili hiç video bulunamadi!</div>');
            }
        }
    });
    }
            $.youtubePlay = function(yid, frame){
    $('.youtubeOynat').slideUp().empty();
    $('#'+yid).slideDown().html('<iframe src="'+ frame +'&autoplay=1" style="width: 100%; box-sizing: border-box; height: 300px" />');
    }
        });
    </script>

    <div class="arama">
    <form action="" onsubmit="return false">
        <h2>Youtube API jQuery AutoComplete</h2>
        <div class="ui-widget">
            <label for="youtube">Youtube SEARCH: </label>
            <input id="youtube" />
            <button id="submit">SEARCH</button>
        </div>
    </form>
    </div>