0

Here is the code - http://jsfiddle.net/and7ey/k2J5v/8/. Once button is pressed, default value (value="0") should be chosen there.

I've tried

$("#cbCity").val("0"); // cbCity - combobox

But it doesn't work. Looks like I also need to change the value of input element.

Such change should not call select.

Upd. I've also tried to do the following:

$("#cbCity").html('<option value="0" selected>Any city</option>');
$("#cbCity").val("0");

It doesn't work.

5
  • Do you need to make your ajax call every time someone clicks the drop down? Or are you fine with loading it once and using that data during the duration of their stay?
    – Jacksonkr
    Commented Nov 19, 2011 at 17:11
  • @Jackson, I think I can load list of most popular cities once, but surely I shoudn't load the whole list.
    – LA_
    Commented Nov 20, 2011 at 9:18
  • @LA Do you have a pretty big list? If so, maybe you could load it synchronously?
    – Jacksonkr
    Commented Nov 21, 2011 at 16:41
  • @Jackson, the list of all possible cities looks big for me :)
    – LA_
    Commented Nov 21, 2011 at 17:25
  • @LA that's a tough one :( It may help to use a cached server response, but there's other problems you might run into there. My knowledge is about capped out on that one. Let me know if there's anything else I can help out with!
    – Jacksonkr
    Commented Nov 21, 2011 at 19:00

3 Answers 3

9
+50

Here's your huckleberry.

What was done:

  • Moved your code from the _create to the page. This way users don't have to wait for the data every time they click. The code happens once on page load and that's it. You may want to move this to a DOM Ready.
  • Made the original select hidden by default so you don't see the jarring change of style to the ui.combobox.
  • Changed how your ui.combobox items were laid out so that "Any City" is at the top.
  • When you click #showAnyCity then both the custom cb and the original cb both reset. The combo selection menu is closed as well (in case it was open).

Happy Coding

3
  • 1
    voteup for thinking bigger than just the immediate problem, good overall recommendations for this situation!
    – BenSwayne
    Commented Nov 19, 2011 at 21:03
  • Good solution, but .find(':first') will always return the first item, and not the selected one. My only change would be: var sel_str = 'any'; $('.ui-autocomplete-input').val($("#cbCity").val(sel_str).find(":selected").val()); in the case of sel_str being any value. EDIT: hit enter instead of shift enter :( Commented Sep 13, 2012 at 16:53
  • @gusterlover6 it's been a while since I wrote this, but my understanding was that the "show Any City" button is just supposed to highlight the element "Any City" which is statically the first element. Hence the breakdown: show Any City
    – Jacksonkr
    Commented Sep 13, 2012 at 21:49
1

Ok so I think there are two things going on:

  1. You're not wireing up the click event to the correct object. You had $("#toggle").click() but you really want $("#showAnyCity").click()
  2. When you change the select list item and your AJAX call comes back, you are not re-adding the 'Any city' and 'Other city' options. If there isn't an option with a value of "0", than .val("0") won't do anything. You either need to add them server side in the results that get sent back in the AJAX call or add them to the select list in the JavaScipt.
2
  • Re 1 - yeah, sorry, just copied the code incorrectly ;). Re 2 - will try that later, thanks.
    – LA_
    Commented Nov 14, 2011 at 20:16
  • Hmm, I've tried option 2 with value="London" (server returns such value), but is doesn't work - jsfiddle.net/and7ey/k2J5v/14. And, I don't know how to add an option on the client side.
    – LA_
    Commented Nov 14, 2011 at 20:24
1

This is what you want I think so!

Now I have done with jquery UI combobox as you need it.

You need to add a function as mentioned below

autocomplete : function(value) {
    this.element.val(value);
    this.input.val(value);
}

And you need to call like this

$('#cbCity').combobox('autocomplete', 'Any city'); 

I have updated with this new one check out this:

http://jsfiddle.net/Fa9Hs/62/

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.