views:

247

answers:

1

I found that using declaring jQuery Autocomplete as the first function in your script prevents other functions from being executed.

The second and third functions below will not work:

<script>
$(function() {
 $("#ent_input").autocomplete('get_suggestions', 
        {autoFill: true, scroll: false});
});

    // Freebase Suggest
$(function() {
 $("#ent_input_fb").suggest({type:'/film/director'});
});

$(function() {
 $("#ent_input_fb2").suggest({type:'/film/actor'});
});
</script>

However, if I move autocomplete to the bottom, the other script works, but autocomplete doesn't.

<script>
    // Freebase Suggest
$(function() {
 $("#ent_input_fb").suggest({type:'/film/director'});
});

$(function() {
 $("#ent_input_fb2").suggest({type:'/film/actor'});
});

    $(function() {
 $("#ent_input").autocomplete('get_suggestions', 
        {autoFill: true, scroll: false});
});
</script>

Has anyone run into any similar issues with jQuery Autocomplete?

+3  A: 

jQuery should execute all of your $(function()...) handlers in the order they have been declared without issue. It looks like your calls to autocomplete and suggest are the culprits.

jQuery is extremely cautious about event handlers, and if you tried to do something similar to the above with click handlers you'll notice that they too execute in sequence (rather than overwrite each other). Try it:

Assume someElement is marked up as follows:

<span id="someElement" onclick="alert('Me First!');" />

...and the JavaScript

$("#someElement").click
(
  function()
  {
    alert("1");
  }
);

$("#someElement").click
(
  function()
  {
    alert("2");
  }
);

You will see three alert boxes shown in the following order: "Me First!," "1," and "2."

jQuery goes the extra mile to retain your marked up onclick handlers as the first function to click when the event occurs. This makes it very safe to use this library with server-side technology like ASP.NET, which sprinkles onclick handlers all over the place.

To remove all jQuery-assigned click event handlers from an element, do the following:

$("#someElement").unbind("click");

Bear in mind that this does not remove a click handler assigned some other way (i.e., the onclick attribute in markup).

David Andres
$(function() { ... }); is a shortcut for $(document).ready(function() { ... });. See http://docs.jquery.com/Core/jQuery#callback
kevingessner
@kevingessner: did you deduct me? That's a bit strong.
David Andres
$(function() {} ) is shorthand for $(document.ready(function() {})). It seems like OP's version should work?
womp
@womp: it should.
David Andres
@David Andres: Thank you for the details. I tried your suggestion and it does work. I think their is an issue with "autocomplete". If I move it to the bottom, any of the functions above it work. If it's at the top, none of the ones below it work.
Wraith
@Wraith Monster: questions (1) I'm not familiar with the suggest library. What is its home page? (2) One of the suggest libraries I saw online required that the Dimensions plugin is referenced as well. See http://www.vulgarisoip.com/2007/06/29/jquerysuggest-an-alternative-jquery-based-autocomplete-library/ (3) I would have expected URLs to be a part of the suggest function so that it knows which location to access to get the suggestions. Is this missing?
David Andres