Lakshmana Kumar
Lakshmana Kumar

Reputation: 1239

Select jQuery autocomplete value by its id

I have 2 text box with jquery autocomplete. My requirement is

If i select Text Box 1 label or value aa and its id is 1, then in Text Box 2 label or value which contain id as 1 that is (aa1) should be selected.

I don't want to copy value or match value in Text Box 1 to Text Box 2 or vice versa.

And also i want to know how to select autocomplete value by passing its id?

Example :

If i select bb (id=2) in Text Box 1 then bb1 (id=2) in text Box 2 should be selected.

If i select cab1 (id=5) in Text Box 2 then cab (id=5) in text Box 1 should be selected.

Fiddle Demo

My HTML and jQuery

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML5//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="css/jquery-ui-start-custom-1.10.3.css" rel="stylesheet" type="text/css" />
    <script src="scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="scripts/jquery-ui-custom-1.10.3.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            var arraytxt1 = [{ id: 1, label: "aa" }, { id: 2, label: "bb" }, { id: 3, label: "bbbb" }, { id: 4, label: "abab" }, { id: 5, label: "cab"}];

            $("#txt1").autocomplete({
                source: arraytxt1,
                minLength: 1,
                select: function(event, ui) {
                    //$("#txt2").val(ui.item.label);
                    // Corresponding Text Box 2 value to selected.
                }
            });

            var arraytxt2 = [{ id: 1, label: "aa1" }, { id: 2, label: "bb1" }, { id: 3, label: "bbbb1" }, { id: 4, label: "abab1" }, { id: 5, label: "cab1"}];

            $("#txt2").autocomplete({
                source: arraytxt2,
                minLength: 1,
                select: function(event, ui) {
                   // Corresponding Text Box 1 value to selected.
                }
            });

        });
    </script>
</head>
<body>
    <div>
        Text Box 1
        <input type="text" id="txt1" />
        Text Box 2
        <input type="text" id="txt2" />
    </div>
</body>
</html>

Upvotes: 1

Views: 6826

Answers (2)

Sruti
Sruti

Reputation: 670

 $("#txt1").autocomplete({
                source: arraytxt1,
                minLength: 1,
                select: function(event, ui) {
                     var matching_right_side_option;
                    $.each(arraytxt2, function(index){
                        if(this.id === ui.item.id) 
                        {
                            matching_right_side_option = this;
                        }    

                    }); 
                 $("#txt2").val(matching_right_side_option.label);
                }
            });

Upvotes: 2

Jatin patil
Jatin patil

Reputation: 4288

To select autocomplete value by its id you will have to use value instead of label:

For eg:

        $("#txt2").autocomplete({
            source: arraytxt2,
            minLength: 1,
            select: function(event, ui) {
              $("#txt1").val(ui.item.value);  //selecting by its id(value)
            }
        });

Upvotes: 0

Related Questions