0

I just created an autocomplete textbox via jquery now i am stuck with two problems.

1) I need to get the value of the selected item from the Autocomplete list.

So far i did these..

My jquery

$(document).ready(function(){
$("#tag").autocomplete("autocomplete.php", {
    selectFirst: true

});
});

I am getting the autocomplete list from the database its working fine, but the problem is that

2) When i type 'r' all the names with values gets populated and if i select say Robin from the list and try to display it with alert, i only gets 'r', (i only typed r,and selected 'robin' from list) why is this so?

here is the code i wrote for this

for the autocomplete textbox

  <input name="tags" type="text" id="tag"  value="" onchange= "newfn()" />

and in newfn()

<script>
        function newfn()
        {

             authname = document.getElementById("tag").value;

            document.autoquote.qid.value=authname;
            alert(authname);

        }
    </script>

and if i put an alert at first saying

alert("Hi");

then, i get first alert saying hi, and I get robin not r

So what is the correct way to get what value i selected from the autocomplete list?

Now my second Question is that the selected value from this autocomplete textbox, I need to pass it with another jquery to another php page so that I can get this value there and give it in a query as a criteria.

1
  • i encoded it into jason and tried to alert it via select in jquery function WALLA! problem solved!!! One more problem how to pass this variable which is obtained from ui.item.label to another jquery autocomplete function? Commented Apr 25, 2014 at 11:00

4 Answers 4

1

Because it is not the way to catch a select event http://api.jqueryui.com/autocomplete/#event-select :

$(document).ready(function(){
    $("#tag").autocomplete("autocomplete.php", {
        selectFirst: true,
        select: function( event, ui ) {
            //Then use "ui" object, for example ui.item.label or ui.item.value                
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

No use what? You need to give more information. Is the select callback not called? Did you check the error console (F12)? ...
i am using netbeans, the output window is the error console right?, if it is so then output window is blank, also i got firebug in there i can see response as robin
1
$(".autosearch-smart").autocomplete('autocomplete.php', {
 select: function( event, ui ) {
    // here you can get id and values from the autocomplete list
   // try to debug
     console.log(ui.item.id);
     console.log(ui.item.label);
     console.log(ui.item.value);
}


});

2 Comments

i copied your code from console and inserted it to my code, the third console code showed error, when i removed it, it ran and i selected robin but the output of netbean showed nothing its blank
no man, still not working, i checked both in netbean output window and also in firebug console.
1

you can use the autocomplete's select property like this

$("#tag").autocomplete('autocomplete.php', {
    selectFirst: true,
    select: function (event, ui) {
        var label = ui.item.label;
        var value = ui.item.value;
        alert(label);
        alert(value)
     // you can write additional javascript code here to make use of these values

    }
 });

Please check the jQuery UI documentation for more examples and info.

Here is a JSFiddle.(Please note that an additional source attribute has been added in the fiddle to simulate the loading of data set as real cross domain AJAX calls are not allowed)

8 Comments

i tried , your answer, but when i select 'robin' from the list, no alert is occuring!! :-(
I had a different selector while applying the autocompleate, may be you copied that only, give me a moment I'll create a jsfiddle and share a link.
@Mukund: I updated the answer with a working fiddle you can check it. :)
did that, but still no alerts in webpage
one more thing to ask is that, will the variables value and label be available to another autocompelte jquery?
|
0
$(document).ready(function(){
$("#tag").autocomplete("autocomplete.php", {
    selectFirst: true
    select: function( event, ui ) {
         //check the values in console    
         alert(ui.item.value+" - "+ui.item.value);      
    }
});
});

1 Comment

in my firebug debugger of firefox i got response as robin! is it? can this value be passed with another autocomplete jquery so that can get it in another php page?

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.