3

I'm using the jQuery UI Autocomplete widget. I need to use the selected text for further processing, direct after change or select.

But when a user clicks the suggested value, the typed value gets passed.

$( "#autocomplete" ).autocomplete({
source: function( req, resp ) {
    $.post( "/echo/json/", {
        json: '["Test1", "Test2", "Test3", "Test4", "Test5"]',
        delay: 1
    }, function(data) {
        resp( data );
    }, "JSON" );
},
select: function (event, ui) { alert($(this).val())}
});

An example at this fiddle: (try typing "tes" and select a suggestion by mouse)

http://jsfiddle.net/M3Yur/

i need to use the input value. a change events uses the input value, it needs to work in both cases when a autocomplete item is selected, but also when just a value is typed so the alert is just for the example.

Any way to get around this?

EDIT: i ended up using: $(this).val(ui.item.value).val(); this solutions always gets the intended value after a change event.

2 Answers 2

4

Change this:

select: function (event, ui) { alert($(this).val())}

To this:

select: function (event, ui) {alert(ui.item.value);}

New fiddle: http://jsfiddle.net/M3Yur/4/

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

3 Comments

Thanx Jon/Jen, The alert is a example, i need to use the input value. a change events uses the input value, it needs to work in both cases when a autocomplete item is selected, but also when just a value is typed
You have both values. Maybe if we knew when the change event fires??
The suggestion did point me in the direction that works for me: $(this).val(ui.item.value).val(); works for me
2

The ui argument contains the information you're looking for:

select: function (event, ui) { alert(ui.item.value); }

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.