1

I have this code for autocomplete in an HTML input :

$("#myinput")
    .bind("keydown", function(event) {
        // don't navigate away from the field on tab when selecting an item
        if (event.keyCode === $.ui.keyCode.TAB 
                                   && $(this).data("autocomplete").menu.active) {
            event.preventDefault();
        }
    })
    .autocomplete({
        minLength: 0,
        source: function(request, response) {
            var results = [],
                selectionStart = this.element[0].selectionStart
                term = extractLast(request.term.substring(0, selectionStart));

                if (term.length > 0) {
                    console.log(term);
                if(/*input has string "where"*/)
                results = $.ui.autocomplete.filter(table1, term);
                else
                results = $.ui.autocomplete.filter(table2, term);   
            }
            response(results);
        },
        focus: function() {
            return false; // prevent value inserted on focus
        },
        select: function(event, ui) {
            var terms = split(this.value.substring(0, this.selectionStart));
            terms.pop();  // remove the current input
            terms.push(ui.item.value);        // add the selected item
            this.value = 
                $.trim(terms.join(" ") + this.value.substring(this.selectionStart)) + " ";
            return false;
        }
    });

What I'm trying to do is if the input has string "where" in somewhere, then it will load autocomplete from table1, otherwise it will load from table2. How can I check if the input has that string? Thanks in advance.

4

4 Answers 4

1

You should use includes method.

var inputValue=$("#myinput").val();
if(inputValue.toLowerCase().includes("where")){
    //rest of code
}

Another method is using indexOf method.

if(inputValue.indexOf("where")!==-1){
    //rest of code
}

If you want to do this achievment using regex, you can use search method.

if(inputValue.search(/where/i)!==-1){
    //rest of code
}

inputValue="awherea";
console.log(inputValue.search(/where/))

Sign up to request clarification or add additional context in comments.

3 Comments

str.search is also applicable
I think indexOf is better since IE doesn't support includes
Could also use match
0

If you want the strongest browser support, use String#indexOf

if(this.value.indexOf('where') > -1) {
   //doSomething
}

Comments

0

you can get your text value and use indexof to find it .

my_inp = $("#myinput").val();
if (my_inp.indexOf('where') > -1) {
   console.log('yes');
}

Comments

0

Try with string#match method use the ternary operator for return true or false

And also Some of the More method

  1. string.indexOf()
  2. string.includes()

console.log('hello everyone'.match("hello") ? true : false)

For jquery you could use the contains() method

console.log($('p').is(':contains("hi")')) 
console.log($('p').is(':contains("22")')) // not contains
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>hello hi</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.