Simple dynamic change... or so i thought. User selects an option from a select list and i update a couple divs based on which item they picked. In my code below the hard coded "Mexico" correctly updates the div with the proper contents. I can't get it to use the value of the variable picked instead. // Set variable to location selected $("select").change(function () { $("select option:selected").each(function () { picked = $(this).text(); // Line below shows the correct data for Mexico // How do i make "Mexico" the variable 'picked' to show the data that was selected? $("div.state").html("<p>Distributors for: " + picked + "</p>"); $("div.locations").html(jQuery.data(div, "location").Mexico); }) }) .change(); Code (markup):
Here's the latest version of my entire page with just the list of option tags shortened. <!DOCTYPE html> <html> <head> <style> div { color:brown; } </style> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <select name="state" size="5"> <option selected="selected">Select a State</option> <option>Mexico</option> <option>Alabama</option> <option>Alaska</option> </select> <div class="state"></div> <div class="locations"></div> <script> // Initial set of global variables var picked = ""; // Set array of text for each state var div = $("div.locations")[0]; jQuery.data(div, "location", { "State" : "", "Mexico" : "<p>Mexico Name<br />Mexican address<br />State<br />Phone</p>", "Alabama" : "This is Alabama", "Alaska" : "They selected Alaska" }); // Set variable to location selected $("select").change(function () { $("select option:selected").each(function () { picked = $(this).text(); // Line below shows the correct data for Mexico // How do i make "Mexico" the variable 'picked' to show the data that was selected? $("div.state").html("<p>Distributors for: " + picked + "</p>"); $("div.locations").html(jQuery.data(div, "location").Mexico); }) }) .change(); </script> </body> </html> Code (markup):
You are trying to get an object property using a variable. Change $("div.locations").html(jQuery.data(div, "location")[picked]);
Thank you, that works perfectly. I had tried square brackets put was also putting in the dot navigation