While browsing on stackoverflow, I came across the following code snippet:
$(document).ready(function () {
$("#show").click(function () {
getYoutube($("#Search").val());
});
});
function getYoutube(title) {
$.ajax({
type: "GET",
url: yt_url = 'http://gdata.youtube.com/feeds/api/videos?q=' + title + '&format=5&max-results=1&v=2&alt=jsonc',
dataType: "jsonp",
success: function (response) {
if (response.data.items) {
$.each(response.data.items, function (i, data) {
var video_id = data.id;
var video_title = data.title;
var video_viewCount = data.viewCount;
$("#result").html(video_id);
});
} else {
$("#result").html('false');
}
}
});
}
Is there a way to modify the code so that only the function remains? I would like to be able to use it like this: getYoutube(my_keywords);
Additionally, how can I store the output of the function in a variable? Something like: var_name = getYoutube(my_keywords); Would that work?
Thanks! ;)