Last active
August 29, 2015 14:25
-
-
Save chucknado/888b42be342d12f30eda to your computer and use it in GitHub Desktop.
A jQuery version of a script for the article "Adding KB search to your website with AJAX" at https://support.zendesk.com/hc/en-us/articles/206287378
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $('#search_btn').click(function(event) { | |
| event.preventDefault(); | |
| var search_string = encodeURIComponent($('#query').val()); | |
| $.ajax({ | |
| url: 'https://your_subdomain.zendesk.com/api/v2/help_center/articles/search.json?query=' + search_string, | |
| contentType: 'application/json', | |
| success: show_results, | |
| error: show_error | |
| }); | |
| }); | |
| function show_error() { | |
| alert('Request error'); | |
| } | |
| function show_results(data) { | |
| clearPreviousResults(); | |
| $('<div id="search_results"></div>').insertAfter('#searchForm'); | |
| if (data.results.length) { // if results exist | |
| $('<ul></ul>').appendTo('#search_results'); | |
| for (var i = 0; i < data.results.length; ++i) { | |
| var article = data.results[i]; | |
| var a_href = '<a href="' + article.html_url + '" target="_blank">' + article.title + '</a>'; | |
| $('<li></li>').html(a_href).appendTo('ul'); | |
| } | |
| } else { | |
| $('<p>No results</p>').appendTo('#search_results'); | |
| } | |
| } | |
| function clearPreviousResults(){ | |
| var results = $('#search_results'); | |
| if (results) { | |
| results.remove(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment