1

I have a street input field with JQuery ui autocomplete suggestions. After the user selected one of the suggestions, he should enter the number. Then on number blur a function validates with below autocomplete search function if the street is in the suggestions. If yes, an other function should be called. I can't call the other function.

Here is my code:

$("#street").autocomplete({ 
   source: "street.php",
minLength: 2,    
    search: function (event,ui) {
            //Return to input field
            if (ui.item==null)
             {
             alert("Please select a street.");          
           $("#street").focus();
           return;
            } else{
                //call an outside function,
                //does not work
                address-search();
            }               
    }
  });

Thank you in advance for help for solving this problem.

2 Answers 2

1

I think you need the search callback to do that:

$("#street").on("autocompletesearch", function(event, ui) {
    if (ui.item === null) {
       alert("Please select a street.");          
       $("#street").focus();
    } else {
       address-search();
    }
});

Documentation here: http://api.jqueryui.com/autocomplete/#event-change

Edit: Based on your specific code, I think you want this:

$(document).ready(function() {
  var availableTags = [
    "1.Street",
    "2.Street",
    "3.Street"
  ];
  $("#street")
    .autocomplete({
      source: availableTags,
      minLength: 2,
    })
    .on("autocompletechange", function(event, ui) {
      if (ui.item === null) {
        alert("Please select a street.");
        $("#street").focus();
        xhr.abort();
      } else {
        address_search();
      }
    });
  function address_search() {
    alert($("#street").val());
  }
});

Updated jsfiddle: https://jsfiddle.net/dupxj3mu/3/

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

6 Comments

Hello, the autocompletesearch does not check correctly and triggers for false results.
Do you have the full code, or a link to a jsfiddle so I can see the problem in action?
Hello, if use in my script this code works but also only after false input. jsfiddle.net/dupxj3mu Maybe because I can not enable Jquery. Thank you for your help.
You'll definitely need jQuery and jQueryUI for autocomplete to work: jsfiddle.net/dupxj3mu/2
Hello tymothytym, thank you for enabling Jquery. Like mentioned, it works only after one wrong input. I would need either a possibility to trigger the function without wrong input or a possibility to stop the search_street function if the street input field has a wrong street name.
|
0

thank you, I found a solution for my own. https://jsfiddle.net/dupxj3mu/5/

function search_street() {
    $("#street").on("autocompletechange", function(event, ui) {
      if (ui.item === null) {
        $("#hiddenstreet").val($("#street").val());
        $("#street").val("");
        return false;
      }
    });
    if ($("#street").val() === "") {
      $("#street").val($("#hiddenstreet").val());    
      $("#street").focus();
      alert("Please select a street.");     
      $("#hiddenstreet").val("");
      return false;
    } else {
      address_search();
    }
  }

But the autocomplete test only run once then again address_search() will be called by wrong street names, too. I will empty the street input field on wrong input and put aside in a DIV that the street input was wrong. Then address_search() will run only on right selection.

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.