I've tried looking for similar cases and I found some but each time, the code is slightly different and I don't manage finding the solution...
I'm using jQuery Autocomplete on my website whith datas from a mysql database.
The results are sorted by categories to display both products and brands in the same input
So I naturaly pasted the example from their website on mine and it works fine!
The generated json look like this
{"label":"Product 1","url":"product.php?id=1","category":"Products"}
My only problem is that I would like results to be clickable. So when the user clicks on a result, an other page loads instead of the default behaviour which is filling the input with the data.
I have created a demo on jsfiddle so you can see what's happening
http://jsfiddle.net/fJ22W (datas are contained in the js here)
Your help is more than welcome, I guess this is not such a big deal but my poor skills in jQuery prevents me to resolve that problem...
Bertrand
For this, we are going to use the jQuery inbuilt function called autocomplete. We will do that in two different stages. Create a basic HTML file and add an input bar to it. Implement the autocomplete functionality.
In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.
Description: Autocomplete enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering.
Here we will make autocomplete textbox, in which make list pre-populated search result with image. This feature we will make by add custom html tag in jQuery UI autocomplete method by add _renderItem. Here we will use __renderItem method, by using this method we will add custom HTML code in autocomplete textbox.
Use the select event:
$( "#search" ).catcomplete({
source: 'suggest_zip.php',
select: function( event, ui ) {
window.location = ui.item.url;
}
});
Obviously you'll want to have some validation around the url etc.
For what it's worth I'd also recommend using the default autocomplete widget and using the events and options rather than trying to inherit from it. Your code will be much cleaner and less chance of odd bugs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With