JavaScript Jquery autocomplete from XML (multiple values) jquery autocomplete from database c#,jquery autocomplete from database,jquery

I have a list of airports in XML format which I would like to use with jquery autocomplete.

The XML file looks like this:

<item code="AAR" airport="Aarhus" country="Denmark" />

When a selection is made, in addition to filling the search input box with "Aarhus" I want to alert the contents of "code".

I have tried like so:

$(function() {
    $.ajax({
      url: "airports.xml",
      dataType: "xml",
      success: function( xmlResponse ) {
        var data = $("item", xmlResponse ).map(function() {
          return {
            value: $( "airport", this ).text(),
            code: $( "code", this ).text()
          };
        }).get();
        $( "#airport_from" ).autocomplete({
          source: data,
          minLength: 0,
          select: function( event, ui ) {
             alert(ui.item.code);
          }
        });
      }
    });
  });

The alert however just shows "undefined", what am I missing?

Answer:1

I think your issue may be that when you give autocomplete a list of objects it expects label and value properties.

http://api.jqueryui.com/autocomplete/#option-source

Maybe try this:

$(function() {
    $.ajax({
      url: "airports.xml",
      dataType: "xml",
      success: function( xmlResponse ) {
        var data = $("item", xmlResponse ).map(function() {
          return {
            label: $( "airport", this ).text(),
            value: $( "code", this ).text()
          };
        }).get();
        $( "#airport_from" ).autocomplete({
          source: data,
          minLength: 0,
          select: function( event, ui ) {
             alert(ui.item.value);
          }
        });
      }
    });
  });
Answer:2

I'm wanting my javascript file to control two html5 files simultaneously. <!DOCTYPE html> <html> <head> <title>tryAgainPage1</title> <meta charset="UTF-8"> &...

I'm wanting my javascript file to control two html5 files simultaneously. <!DOCTYPE html> <html> <head> <title>tryAgainPage1</title> <meta charset="UTF-8"> &...

My html code as below: <div class="formContent"> <label for="title">Data Collection Title:</label><p>Western Australia</p><br/> <...

My html code as below: <div class="formContent"> <label for="title">Data Collection Title:</label><p>Western Australia</p><br/> <...

  1. javascript nested element
  2. js nested element
  3. javascript get nested element
  4. javascript select nested element
  5. javascript get nested element by class
  6. javascript find nested element
  7. javascript get nested element by id
  8. javascript find nested element by class
  9. javascript create nested element
  10. javascript remove nested element
  11. javascript target nested element
  12. javascript find nested element by id
  13. javascript scroll nested element
  14. javascript get nested element by tag
  15. javascript access nested element
  16. javascript nested array element
  17. javascript hide nested element
  18. javascript update nested element
  19. js get nested element
  20. js select nested element

I have been using getUserMedia() for WebRtc for a while now but since the latest update of browsers I have not been able to use this. On previous versions worked fine. Affected browsers' versions ...

I have been using getUserMedia() for WebRtc for a while now but since the latest update of browsers I have not been able to use this. On previous versions worked fine. Affected browsers' versions ...

I have a button that has a drawable XML file set as the background, this is so that when the button is pressed the background colour and outline of the button is change. However it does not stay like ...

I have a button that has a drawable XML file set as the background, this is so that when the button is pressed the background colour and outline of the button is change. However it does not stay like ...

  1. change button state swift
  2. change button state tkinter
  3. change button state javascript
  4. change button state android
  5. change button state unity
  6. change button state arduino
  7. change button state react
  8. change button state
  9. change button state html
  10. change button state java
  11. change button state uibutton
  12. change state button click
  13. unity change button state script
  14. nextion change button state
  15. blynk change button state
  16. android change button state programmatically
  17. jquery change button state
  18. labview change button state
  19. change toggle button state android
  20. change radio button state javascript