1
votes

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
you mean value in your input box ?Mohideen bin Mohammed
@MohideenibnMohammed yes.jason
check my answer.. you can use indexof to find itMohideen bin Mohammed

4 Answers

1
votes

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/))
0
votes

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

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

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');
}
0
votes

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>