I'm looking for a better way to handle dynamic populating of my options using JS and JQuery. What I have is working but I am looking for a way to not have t ore-write the function each time I need to populate a list.
And what I am doing is populating these:
<label for="recordPurchaseTimeFrameID" class="input required">When would you like to move?</label> <select name="recordPurchaseTimeFrameID" id="recordPurchaseTimeFrameID" class="inputclass pageRequired" title="Select a Time Frame">
<label for="recordPurchasePriceRangeID" class="input required">Purchase price range:</label> <select name="recordPurchasePriceRangeID" id="recordPurchasePriceRangeID" class="inputclass pageRequired" title="Select a Price Range"> Using these scripts:
var rPTJsonListItems= ""; for (var i = 0; i < rPTJsonList.recordPurchaseTimeTable.length; i++){ rPTJsonListItems+= "<option value='" + rPTJsonList.recordPurchaseTimeTable[i].recordPurchaseTimeValue + "'>" + rPTJsonList.recordPurchaseTimeTable[i].recordPurchaseTimeAmount + "</option>"; }; $("#recordPurchaseTimeFrameID").html(rPTJsonListItems); var rPPJsonListItems= ""; for (var i = 0; i < rPPJsonList.recordPurchasePriceTable.length; i++){ rPPJsonListItems+= "<option value='" + rPPJsonList.recordPurchasePriceTable[i].recordPurchasePriceValue + "'>" + rPPJsonList.recordPurchasePriceTable[i].recordPurchasePriceAmount + "</option>"; }; $("#recordPurchasePriceRangeID").html(rPPJsonListItems); And using this to populate the dropdowns:
var rPTJsonList = { "recordPurchaseTimeTable" : [ {"recordPurchaseTimeValue" : "","recordPurchaseTimeAmount" : "Select"}, .... ]}; var rPPJsonList = { "recordPurchasePriceTable" : [ {"recordPurchasePriceValue" : "","recordPurchasePriceAmount" : "Select"}, {"recordPurchasePriceValue" : "75k-100k","recordPurchasePriceAmount" : "$75,000 - $100,000"}, .... }); So what I'd like is to have just one main function that populates each unique ID based on it's correlating JSON.
Anyone have any suggestions?