Autocomplete widget - not triggering onchange

Autocomplete widget - not triggering onchange

Hi,

First of all, I apologize if I've posted this in the wrong forum. If so, could a moderator please move it.

Now to my dilemma. I am trying to develop a textbox and a dropdown, where the value of the textbox is dependent on the option selected from the dropdown.

For the dropdown I wanted to use the autocomplete feature and found a widget that does it (from here:  Link)
For the textbox dependency I am using Javascript.

The problem I face is that when using the autocomplete widget, on selecting an option, it is not triggering an onchange event in the select (dropdown). And I need this onchange event for my textbox to be filled in.

There is also a button on the page, which lets you the original select box.

I have put the code for my page below. I'm also using some CSS which I have added at the end.

HTML part:
  1. <html lang="en">
  2. <head>
  3.   <meta charset="utf-8" />
  4.   <title>jQuery UI Autocomplete - Combobox</title>
  5.   <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
  6.   <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
  7.   <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>

  8.   
  9.   <link rel="stylesheet" type="text/css" href="new.css">
  10.   <script src="new.js"></script>
  11.   
  12. <SCRIPT type='text/javascript' >
  13.   //this function determines how changing the value of the dropdown will affect the content of the textboxes
  14. function DeviceInfo() {
  15.  var sel = document.getElementById("combobox").selectedIndex; //gets the index of the select
  16.  document.getElementById("model").value = document.getElementById("combobox").selectedIndex; //displays the index of the select in the textbox
  17. }
  18. </script>
  19.   
  20.   
  21.   
  22. </head>
  23. <body>
  24.  
  25.   <label>Your preferred programming language: </label>
  26.   <select id="combobox" onchange='DeviceInfo()'>
  27.     <option value="">Select one...</option>
  28.     <option value="Asp">Asp</option>
  29.     <option value="BASIC">BASIC</option>
  30.     <option value="C++">C++</option>
  31.   </select>
  32.  
  33. <button id="toggle">Show underlying select</button>

  34. <input type='text' id='model' />
  35.  
  36.  
  37. </body>
  38. </html>

JQuery part (new.js)
  1. (function( $ ) {
  2.     $.widget( "ui.combobox", {
  3.       _create: function() {
  4.         var input,
  5.           that = this,
  6.           wasOpen = false,
  7.           select = this.element.hide(),
  8.           selected = select.children( ":selected" ),
  9.           value = selected.val() ? selected.text() : "",
  10.           wrapper = this.wrapper = $( "<span>" )
  11.             .addClass( "ui-combobox" )
  12.             .insertAfter( select );
  13.  
  14.         function removeIfInvalid( element ) {
  15.           var value = $( element ).val(),
  16.             matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
  17.             valid = false;
  18.           select.children( "option" ).each(function() {
  19.             if ( $( this ).text().match( matcher ) ) {
  20.               this.selected = valid = true;
  21.               return false;
  22.             }
  23.           });
  24.  
  25.           if ( !valid ) {
  26.             // remove invalid value, as it didn't match anything
  27.             $( element )
  28.               .val( "" )
  29.               .attr( "title", value + " didn't match any item" )
  30.               .tooltip( "open" );
  31.             select.val( "" );
  32.             setTimeout(function() {
  33.               input.tooltip( "close" ).attr( "title", "" );
  34.             }, 2500 );
  35.             input.data( "ui-autocomplete" ).term = "";
  36.           }
  37.         }
  38.  
  39.         input = $( "<input>" )
  40.           .appendTo( wrapper )
  41.           .val( value )
  42.           .attr( "title", "" )
  43.           .addClass( "ui-state-default ui-combobox-input" )
  44.           .autocomplete({
  45.             delay: 0,
  46.             minLength: 0,
  47.             source: function( request, response ) {
  48.               var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
  49.               response( select.children( "option" ).map(function() {
  50.                 var text = $( this ).text();
  51.                 if ( this.value && ( !request.term || matcher.test(text) ) )
  52.                   return {
  53.                     label: text.replace(
  54.                       new RegExp(
  55.                         "(?![^&;]+;)(?!<[^<>]*)(" +
  56.                         $.ui.autocomplete.escapeRegex(request.term) +
  57.                         ")(?![^<>]*>)(?![^&;]+;)", "gi"
  58.                       ), "<strong>$1</strong>" ),
  59.                     value: text,
  60.                     option: this
  61.                   };
  62.               }) );
  63.             },
  64.             select: function( event, ui ) {
  65.               ui.item.option.selected = true;
  66.               that._trigger( "selected", event, {
  67.                 item: ui.item.option
  68.               });
  69.             },
  70.             change: function( event, ui ) {
  71.               if ( !ui.item ) {
  72.                 removeIfInvalid( this );
  73.               }
  74.             }
  75.           })
  76.           .addClass( "ui-widget ui-widget-content ui-corner-left" );
  77.  
  78.         input.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
  79.           return $( "<li>" )
  80.             .append( "<a>" + item.label + "</a>" )
  81.             .appendTo( ul );
  82.         };
  83.  
  84.         $( "<a>" )
  85.           .attr( "tabIndex", -1 )
  86.           .attr( "title", "Show All Items" )
  87.           .tooltip()
  88.           .appendTo( wrapper )
  89.           .button({
  90.             icons: {
  91.               primary: "ui-icon-triangle-1-s"
  92.             },
  93.             text: false
  94.           })
  95.           .removeClass( "ui-corner-all" )
  96.           .addClass( "ui-corner-right ui-combobox-toggle" )
  97.           .mousedown(function() {
  98.             wasOpen = input.autocomplete( "widget" ).is( ":visible" );
  99.           })
  100.           .click(function() {
  101.             input.focus();
  102.  
  103.             // close if already visible
  104.             if ( wasOpen ) {
  105.               return;
  106.             }
  107.  
  108.             // pass empty string as value to search for, displaying all results
  109.             input.autocomplete( "search", "" );
  110.           });
  111.  
  112.         input.tooltip({
  113.           tooltipClass: "ui-state-highlight"
  114.         });
  115.       },
  116.  
  117.       _destroy: function() {
  118.         this.wrapper.remove();
  119.         this.element.show();
  120.       }
  121.     });
  122.   })( jQuery );
  123.  
  124.   $(function() {
  125.     $( "#combobox" ).combobox();
  126.     $( "#toggle" ).click(function() {
  127.       $( "#combobox" ).toggle();
  128.     });
  129.   });
CSS part (new.css)
  1. .ui-combobox {
  2.     position: relative;
  3.     display: inline-block;
  4.   }
  5.   .ui-combobox-toggle {
  6.     position: absolute;
  7.     top: 0;
  8.     bottom: 0;
  9.     margin-left: -1px;
  10.     padding: 0;
  11.     /* support: IE7 */
  12.     *height: 1.7em;
  13.     *top: 0.1em;
  14.   }
  15.   .ui-combobox-input {
  16.     margin: 0;
  17.     padding: 0.3em;
  18.   }
I hope I have been able to explain this properly. Could someone please help me figure out how to trigger the onchange event? And if can't be done, is there another way to do what I want?


Thanks,
fractal5