Reputation: 1
I want to show all google search auto completes in a textbox. After searching google i found "http://google.com/complete/search?output=toolbar&q=test" link that is return a xml data contains ten google search suggets. My jQuery code for showing this xml values is :
$(window).ready(function(){
$( "#textbox" ).keypress = showSuggest();
});
function showSuggest() {
$.ajax({
type: "GET",
url: "http://google.com/complete/search?output=toolbar&q=" + $("#textbox").val(),
dataType: "xml",
var data = [];
success: function(xml) {
$(xml).find('CompleteSuggestion').each(function(){
data.push($(this).find('suggestion')[0].attr('data'));
});
}
});
$( "#textbox" ).autocomplete({
source: data
});
}
jquery-1.9.1.js and jquery-ui-1.10.3 was imported but this code not working. sorry for my bad english. thanks.
UPDATE
thanks to everyone. i edit my code and remove xml reading part and replace
url: "http://google.com/complete/search?output=toolbar&q=" + $("#textbox").val()
$("#textbox").autocomplete({
source: data
});
with this :
$( "#textbox" ).autocomplete({
source: "http://suggestqueries.google.com/complete/search?client=firefox&q=" + $( "#textbox" ).val()
});
now on typing text only show progress image left side of textbox and still not showing suggets. please help.
NEW UPDATE
I write new code with firefox DOMParser but still not working.
$("#textbox").keypress(function() {
var data = [];
var parser = new DOMParser();
var xml = parser.parseFromString("http://google.com/complete/search?output=toolbar&q=" + $("#new-tag-post_tag").val(), "application/xml");
xml.domain = "google.com";
suggests = getElementsByTagName("CompleteSuggestion");
for (var i = 0; i < suggests.length; i++) {
data.push(suggests[i]);
}
$( "#textbox" ).autocomplete({
source: data
});
}
Upvotes: 0
Views: 2798
Reputation: 754
your javascript syntax is wrong. This could work:
$(document).ready(function() {
$("#textbox").on("keypress", showSuggest);
});
function showSuggest() {
var data = [];
$.ajax({
type: "GET",
url: "http://google.com/complete/search?output=toolbar&q=" + $("#textbox").val(),
dataType: "xml",
success: function(xml) {
console.log(xml);
$(xml).find('CompleteSuggestion').each(function(){
data.push($(this).find('suggestion')[0].attr('data'));
});
$( "#textbox" ).autocomplete({ source: data});
}
});
}
anyway I recommend you to learn JavaScript before you start programming.
Upvotes: 0
Reputation: 3353
$(document).ready(function(){
$( "#textbox" ).keypress(showSuggest);
});
function showSuggest() {
// Declare the variable here, not inside the $.ajax code
var data = [];
$.ajax({
type: "GET",
url: "http://google.com/complete/search?output=toolbar&q=" + $("#textbox").val(),
dataType: "xml",
success: function (xml) {
$(xml).find('CompleteSuggestion').each(function () {
data.push($(this).find('suggestion')[0].attr('data'));
$("#textbox").autocomplete({
source: data
});
});
}
});
}
Upvotes: 1