Skip to content

Instantly share code, notes, and snippets.

@jbutko
Last active September 20, 2016 21:04
Show Gist options
  • Save jbutko/81658ac86ac730fcf8c4 to your computer and use it in GitHub Desktop.
Save jbutko/81658ac86ac730fcf8c4 to your computer and use it in GitHub Desktop.
jQuery, AJAX: Instant AJAX Search (Bonus: SoundCloud API Search)
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- SoundCloud API -->
<script src="//connect.soundcloud.com/sdk.js"></script>
<script>
SC.initialize({
client_id: " insert your client ID",
redirect_uri: " insert redirect URL",
});
</script>
<!-- SoundCloud API End -->
<h1>Search soundCloud:</h1>
<form action="index_submit" method="get" accept-charset="utf-8">
<input id="search" type="search" name="search">
<input id="submit" type="button" name="submit" value="Search">
</form>
<!-- Here we display AJAX search results -->
<div id="getpocket"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
</body>
</html>
jQuery(document).ready(function($) {
$("#search").keyup(function(){
// get the search form value
var formData = $("#search").val();
// clear search results div
$('#getpocket').html('');
// find all tracks with the genre 'punk' that have a tempo greater than 120 bpm.
SC.get('/tracks', { q: formData, duration: { from: 3000000 }, limit: '40' }, function(tracks) {
for (var i=0, len=tracks.length; i < len; i++) {
$('#getpocket').append('<div>' + '<a href="' + tracks[i].permalink_url + '">' + tracks[i].title + '</a>'+ '</div><br>');
var track_url = tracks[i].permalink_url;
var player = SC.oEmbed(track_url, { auto_play: true } );
$('#getpocket').html(player);
}
});
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment