Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:41:03+00:00 2026-06-17T04:41:03+00:00

Pavel K’s answer worked. However, for an unknown reason I needed to change the

  • 0

Pavel K’s answer worked. However, for an unknown reason I needed to change the “data-stationID” to something like “data-sid”. May be a mixed case issue?

For anyone else needing more than one piece of data, this line shows how to “stack” more data into the option;

$('<option data-stationID="' +$(this).data('sid')+ '" data-lat="' +$(this).data('lat')+'" data-lon="' +$(this).data('lon')+'"  ></option>').text($(this).data('city')).appendTo(city);

My XML:

<states>
<state name="AK"> 
        <option value="null" data-stationID="null" data-lat="null" data-lon="null" data-city="Select City">Select City</option>
        <option value="326" data-stationID="9450460" data-lat="55.331799" data-lon="-131.626205" data-city="Ketchikan">Ketchikan</option>
        <option value="327" data-stationID="9451054" data-lat="56.246700" data-lon="-134.647003" data-city="Port Alexander">Port Alexander</option>
</state>
<state name="CT"> 
        <option value="null" data-stationID="null" data-lat="null" data-lon="null" data-city="Select City">Select City</option>
        <option value="35" data-stationID="8461490" data-lat="41.361401" data-lon="-72.089996" data-city="New London">New London</option>
        <option value="36" data-stationID="8465705" data-lat="41.283298" data-lon="-72.908302" data-city="New Haven">New Haven</option>
</state>

My current code:

$('#states').change(function() { // bind a trigger to when the states select changes
        var val = $(this).val(); // hold the select's new value
        var city = $('#cities').empty(); // empty the select of cities and hold a reference in 'city'
        $('state', station_data).filter(function() { // get all states...
            return val == $(this).attr('name'); // find the chosen state
        }).find('option').each(function() { //  Search Between/Within "<option..." for data ending in "city" & create a new option, set its text to the city [name], append to the cities select            
            $('<option>').text($(this).data('city')).appendTo(city);

        })
    });

$('#cities').change(function() { // bind a trigger to when the cities select changes
        console.log($(this).val() );// prints selected city


    });

My question is; How do I access the data like “data-stationID” after the user has selected a city? At best, I seem to be only able to get either all stationID’s -or only the last stationID inside the option tags.

The idea is that after the user selects the city they’d like information on, I’d have access to all the other “data-*” stored between the option tags so I could query my db [or another php page]

I used the code from dynamic load data from xml to drop down box with jquery to start this.

Thanks for any help!

Per Ohgodwhy’s request, This is my php code that queries my db.

fwrite($fh,  "<?xml version=\"1.0\"?>\n"); 
fwrite($fh,  "<states>\n");
while($row=mysqli_fetch_array($states)) {
    fwrite($fh,  "  <state name=\"${row['state']}\"> \n");
    $queryCities="SELECT * FROM locations WHERE state='${row['state']}'";
    $cities=mysqli_query($dbc,$queryCities);
    fwrite($fh, "           <option value=\"null\" data-stationID=\"null\" data-lat=\"null\" data-lon=\"null\" data-city=\"Select City\">Select City</option>\n");
    while($row=mysqli_fetch_array($cities)) {
        fwrite($fh,  "          <option value=\"${row['id']}\" data-stationID=\"${row['stationID']}\" data-lat=\"${row['latitude']}\" data-lon=\"${row['longitude']}\" data-city=\"".StripStuff($row['city'])."\">".StripStuff($row['city'])."</option>\n");
    }
    fwrite($fh,  "  </state>\n\n");
}
fwrite($fh,  "</states>\n");    
fclose($fh);

To clarify, I guess I don’t know how JQuery handles this info, so I don’t know how to access. Also, I can obviously rewrite the php code to export any format if there’s a better way I should be formatting the data…

This is my XML import/load function:

$.get('test2.xml', function(data) { // get the states.xml file
        station_data = data; // save the data for future use so we don't have to call the file again
        var state = $('#states'); // states select
        $('state', station_data).each(function() { // find states in data & dynamically create a new option element & make its text the value of the "title" attribute in the XML & append it to the states select
            $('<option>').text($(this).attr('name')).appendTo(state);
        });
    }, 'xml'); // specify what format the request will return - XML

Using JQuery version: jquery-1.8.2.min.js

Using JQuery Mobile version: jquery.mobile-1.2.0.min.js

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:41:04+00:00Added an answer on June 17, 2026 at 4:41 am

    I think that is not clear way, but you can add option tag from XML directly to select. Try this, it should work:

    Update:

    $('#states').on('change', function() { // bind a trigger to when the states select changes
            var state = $(this).val(); // hold the select's new value
            var city = $('#cities').empty(); // empty the select of cities and hold a reference in 'city'
            $('state', station_data).filter(function() { // get all states...
                return state == $(this).attr('name'); // find the chosen state
            }).find('option').each(function() { //  Search Between/Within "<option..." for data ending in "city" & create a new option, set its text to the city [name], append to the cities select            
                $('<option data-stationID="'+$(this).data('stationID')+'"></option>').text($(this).data('city')).appendTo(city);
            })
        });
    
    $('#cities').on('change', function() { // bind a trigger to when the cities select changes
            console.log($(this).children('option:selected:eq(0)').attr('data-stationID') );// prints selected city's attr
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Control Panel -> Security Center I really like the components/controls which are used to
On Pavel's page is the following function: CREATE OR REPLACE FUNCTION makedate(year int, dayofyear
Hi I'd like know how to format BigDecimal representing numbers,percents,moneys,integers to String in Locales
I have a problem with persisting data via play-framework. Maybe it's not possible to
I was using a foreach loop to go through a list of data to
I'm looking at Pavel's solution to Project Euler question 24, but can't quite work
Control Panel items are normally registered under HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\ControlPanel\NameSpace , however a lot of the
var panel:Panel = new Panel(); panel.title = source.displayValue + - + data.displayValue; panel.percentWidth =
I have a grid.panel on the side with table names and I'd like to
panel({}) and all its contents like grid, form and want to render that exact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.