Randel Ramirez
Randel Ramirez

Reputation: 3761

How to set jquery ui combobox selected value or default text?

I have an autocomplete combobox that contains options from mysql, the combobox contains the names of supplier and it's id wherein once the user selects from the combobx the id will be stored in a hiddenfield and then stored in a session. What I want to know is how do I set the default text/value of the autocomplete combobx based from the session?

Ex. Session = 25; SupplierText = "Stackoverflow" ID = 25 When the user clicks on a new tab, the page will autcomatically display the combobox with the appropriate text based from the Session. In this case the combobox should have the text value of Stackoverflow. Just be able to display the text will actually be enough.

Sir/Ma'am your answers would be of great help.

Upvotes: 1

Views: 17057

Answers (3)

HeidiH
HeidiH

Reputation: 31

If you just want to set the value of the combobox input after the combobox is created, you can set the value of the select and then...

$(".ui-combobox-input").val($("#select option:selected").text()); 

Upvotes: 0

Shathur
Shathur

Reputation: 1525

I found out that the option that has the "selected" attribute when the combobox is created, will be selected in the combobox. You can set the selected option by destroying the combobox, removing the "selected" attribute from the option and adding the attribute to the option you want to select, and then creating the combobox again.

Here's my code:

// Destroy the combobox
$(".combobox").combobox("destroy");

// Unselect the currently selected option
$("#selectlist option:selected").removeAttr("selected");

// Select the option you want to select
$("#selectlist option[value='test']").attr("selected", "selected");

// Create the combobox again
$(".combobox").combobox();

Upvotes: 2

masadi zainul
masadi zainul

Reputation: 397

maybe you need example like this

like example :

$("#yourselector").autocomplete({
            source: "your URL",
            minLength: 2,
            select: function(event, ui) {
                $('#selector').val(ui.item.YourDataResponse);
                $('#selector').val(ui.item.YourDataResponse);
            }
});

ui.item is default from JQuery Ui..

Upvotes: 1

Related Questions