geesgee geesgee - 26 days ago 13
Javascript Question

I'm struggling to change my JS code to run URL instead of currently showing and output div

My JS below runs from my search bar and shows results in a same page DIV. I would like to edit this to take them to the page selected from the search bar maybe even using a separate JSON file, or not, either way.

$(function(){
var url = [
{ value: 'Home', data: 'http://google.com' },
{ value: 'Guide', data: 'http://google.com' },
{ value: 'Examples', data: 'ttp://google.com' },
{ value: 'Themes', data: 'http://google.com' },
{ value: 'Download', data: 'http://google.com' },
];

// setup autocomplete function pulling from currencies[] array
$('#autocomplete').autocomplete({
lookup: url,
onSelect: function (suggestion) {
var thehtml = '<strong>Page Name:</strong> ' + suggestion.value + ' <br> <strong>URL:</strong> ' + suggestion.data;
$('#outputcontent').html(thehtml);
}
});


});

Answer

You can use window.location.assign to load the URL as a new document.

// setup autocomplete function pulling from currencies[] array
$('#autocomplete').autocomplete({
     lookup: url,
     onSelect: function (suggestion) {
         window.location.assign(suggestion.data);
     }
 });

Alternatively, you can use window.location.href = suggestion.data;

Comments