Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:46:37+00:00 2026-06-14T00:46:37+00:00

I’m using http://jsfiddle.net/dhoerster/BXYpt/ from jQuery UI autocomplete with values as the basis. This does

  • 0

I’m using http://jsfiddle.net/dhoerster/BXYpt/ from jQuery UI autocomplete with values as the basis. This does exactly what I need it to except that I need to be able to have OR conditions. I’ve created a fiddle as a demo:

http://jsfiddle.net/B8bWX/1/

My issue is this: the demo items are Car, Phone, and Car With Phone. Is there a way to make autocomplete handle three cases: the string contains case (the default) followed by an ANDed case and lastly by an OR case? So if I enter “Car Phone” it would result in:

1) first case – 0 results as “Car Phone” isn’t a substring of any of the labels.

2) second case – 1 result because “Car” and “Phone” appear in “Car With Phone”. This gets added to the list below the 0 results preceding.

3) third case – 2 more results are displayed because the labels “Car” and “Phone” contain at least one of the search items. These results appear below the previous results of stages 1 and 2 so the selection box below should now render:

Car With Phone

Car

Phone (Car and Phone order really don’t matter as they’re both equally right)

Here is the javascript in question:

$(document).ready(function() {
$( "#topics" ).autocomplete({
    minLength: 1,
    source: topics,
    focus: function( event, ui ) {
        $( "#topics" ).val( ui.item.label );
        return false;
    },
    select: function( event, ui ) {
        $( "#topics" ).val( ui.item.label );
        $("#topicID").val(ui.item.id);
        $( "#results").text($("#topicID").val());    
        return false;
    }
 })

});

So would it be possible to have it do in order:

1) string.contains (default)

2) string split –> AND condition

3) string split –> OR condition (no preference to sorting by the number of terms contained)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:46:38+00:00Added an answer on June 14, 2026 at 12:46 am

    What we’ll have to do is do our own search on the data for the “AND” and “OR” results. My first thought was to do this using the response event of the autoselect, and add the results to the ones gathered by the library itself. However, when writing it, I realized that I had to do the normal search anyway in order to prevent results showing up twice. Instead, I’ll be specifying a function for the source property and do all the sorting myself.

    (Using the earlier method, I also ran into a jquery-ui bug (with the new messages showing how many results were found) which does not come up with the newer approach.)

    Well, it’s pretty straight forward from here, so let me just show the code:

    var topics = [
        {
        value: "carphone",
        label: "Car With Phone",
        id: "1"},
    {
        value: "car",
        label: "Car",
        id: "2"},
    {
        value: "phone",
        label: "Phone",
        id: "3"}
    ];
    
    $(document).ready(function() {
        $("#topics").autocomplete({
            minLength: 1,
            focus: function(event, ui) {
                $("#topics").val(ui.item.label);
                return false;
            },
            select: function(event, ui) {
                $("#topics").val(ui.item.label);
                $("#topicID").val(ui.item.id);
                $("#results").text($("#topicID").val());
                return false;
            },
            source: function(request, response) {
                var fullResults = [];
                var andResults = [];
                var orResults = [];
                var fullNeedle = request.term;
                var needles = $.grep(fullNeedle.split(" "), function(element) {
                    return element !== '';
                });
    
                $.each(topics, function(key, topic) {
                    var found = 0;
                    $.each(needles, function(needleKey, needle) {
                        if (topic.label.toLowerCase().indexOf(needle.toLowerCase()) != -1) {
                            found++;
                        }
                    });
    
                    if (topic.label.toLowerCase().indexOf(fullNeedle.toLowerCase()) != -1) {
                        fullResults.push(topic);
                    }
                    else if (found == needles.length) {
                        andResults.push(topic);
                    }
                    else if (found > 0) {
                        orResults.push(topic);
                    }
                });
    
                $.merge(fullResults, andResults);
                $.merge(fullResults, orResults);
                response(fullResults);
            }
        });
    });
    

    A little explanation: we don’t give our topics to the library anymore as the source, but instead we’re keeping the source for ourselves as we search through it ourselves. We do this using three arrays, each one of which will contain one of the three types of results, so we can keep the order correct. Finally, we merge the three arrays after we’re done populating them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.