2

I am creating a searchbox by using jQuery autocomplete.I want to trigger a click event when I click on a corresponding li from the autocomplete list. I am writing as:

  $('.ui-menu-item').click(function(){
                 $('.college').trigger('click');
  });

But It is not working. any Idea ? jsFiddle : http://jsfiddle.net/hn838/

7
  • Does $('.college').click(); not work? Commented Apr 26, 2013 at 20:08
  • Could you post a JSFiddle of what you have? Commented Apr 26, 2013 at 20:11
  • Your code should work plenty fine. What kind of element is .college? Commented Apr 26, 2013 at 20:14
  • @lan .college is a div, in which I have a click event, but It is not firing in case of $('.ui-menu-item'). Otherwise it is working fine. Commented Apr 26, 2013 at 20:17
  • Does the handler for <div class="college"/> work as expected when you actually click on the div? Commented Apr 26, 2013 at 20:25

1 Answer 1

4

At the time your event binding is executed there will be nothing to bind it to because there has been no .ui-menu-item created in the DOM.

You need to code your event as such:

$('.ui-autocomplete').on('click', '.ui-menu-item', function(){
    $('.college').trigger('click');
});

Here is the jsfiddle: http://jsfiddle.net/hn838/4/

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

5 Comments

The second parameter doesn't make sense. It should be a selector, not a jQuery object
@Christopher.Cubells I already updated. It works because it interprets the second parameter as a different on overload and passes it as data. This event handler you've is basically just a click handler for .ui-autocomplete
@lan How it should be like ?
@pandit I updated the answer and fiddle. It was very close, and worked (obviously), but wasn't doing what was intended.
Excellent - this helped me. I've got a 'combobox' handler in a generic js file, and individual js files for specific pages. This snippet allowed me to add this functionality to my individual pages and customise for a specific task. Thanks.

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.