Updated code:
Thanks to some great help the query is now being displayed in a select menu. Now I just need help figuring out how to 'use' the objects that correspond to the selected HosName in .hospital. For example: How could I implement:
$('.hopeful').html(SPval);
where SPval is a JSON value that corresponds to the HosName selection made in .hospital.
From my original post:
As you can see I have a set of JSON data that simply lists multiple hospitals in three different states. In addition, each hospital also has a related set of data values in the JSON file. Currently a user selects a state from the select menu and a corresponding hospital in the same state is listed, but I need to list ALL hospitals in the selected state, not just one.
In addition, the hospitals that are listed need to be listed as links so a user can choose a hospital to use in the application and all of its related values also be available for use as objects later.
Here is a summary of the workflow:
- User selects a state in the dropdown and presses the submit button
- Pressing the submit button runs the function that searches the JSON data and finds all of the entries that match the state value that was selected in the previous step
- ALL HosName values that match the selected state are listed as links
- User chooses a HosName from the list and all JSON values related to the chosen HosName are brought into the application for later use. For example: If a user chooses "YORK HOSPITAL" from the list of hospital names the related strings (SPval,Hospitalval,ICUval,SMval,OPval,AVLOSval) will also be retrieved from the JSON data and be available as objects with the application for later calculations.
New code:
<div>
<select name="state">
<option value="" selected="selected">Select a State</option>
<option value="LA">Louisiana</option>
<option value="TX">Texas</option>
<option value="WI">Wisconsin</option>
</select>
</div>
<select class="hospital"></select>
<p class="hopeful"></p>
var SPval;
var json = {
"hospitals": {
};
$(function () {
$("select[name='state']").change(function () {
var searchName = $("select[name='state']").val();
var matches = $.grep(json.hospitals.facility, function (v) {
return v.State == searchName;
})
var name = $.map(matches, function (v) {
return "<option data-data='" + JSON.stringify( v )+ "'>" + v.HosName + "</option>"
}).join("")
$('.hospital').html(name);
$('.hopeful').html(SPval);
});
});