I have been looking at filling out a form with previous information using javascript, and am able to get the textboxes filled, but the dropdown selections are causing issues. I'm following this method of selecting a specific option, but it doesn't seem to work with the rest of my code. It works only if I create a separate page without the for loop to fill out the textboxes. I've checked the names and id's of the selections to see if there was an overlap somewhere else in my code, but that does not appear to be the case. I originally thought that I solved the issue because I misunderstood the difference between the value of the option versus the index of the option. But that changed nothing. I've been using console.log to verify that each step is being called, and that each input is appropriate. I've tried commenting out the code and just setting the element statically.
The issue I'm having is that the drop down boxes don't populate when I select a profile to edit. The textboxes fill with the last saved data, but nothing appears in the dropdowns. What would cause a dropdown to not populate?
This is the html:
<!-- Edit Button to edit a profile -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="editModalLabel">Edit Profile</h4>
</div>
<div class="modal-body">
<form method="get" name="editprofiledata" id="editprofiledata">
<div >
<label>Details</label>
<table id="tableID" class="table table-bordered table-hover tablewithtooltip">
<thead>
<tr style="background-color: #787878 ">
<th>Attribute</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr><td>Profile Name</td><td><input type="text" value="" name="selectedProfileName" id="selectedProfileName"/></td></tr>
<tr><td>Profile Mnemonic</td><td><input type="text" value="" name="profileMnemonic" id="profileMnemonic"/></td></tr>
<tr><td>Tx Data Rate</td><td><select name="txDataRate" id="txDataRate"><option value="1">64 kbps 1/9</option>
<option value="2">128 kbps 2/9</option>
<option value="3">256 kbps 1/9</option>
<option value="6">256 kbps 2/9</option>
<option value="5">512 kbps 2/9</option>
<option value="4">512 kbps 1/9</option></select></td></tr>
<tr><td>Rx Data Rate</td><td><select name="rxDataRate" id="rxDataRate"><option value="1">64 kbps 1/9</option>
<option value="2">128 kbps 2/9</option>
<option value="3">256 kbps 1/9</option>
<option value="6">256 kbps 2/9</option>
<option value="5">512 kbps 2/9</option>
<option value="4">512 kbps 1/9</option></td></tr>
</tbody>
</table>
</div>
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default btn-default" data-dismiss="modal" name="updateProfile" id="updateProfile" onclick="saveUpdatedProfile()">Save</button>
</div>
</div><!-- /.modal-content -->
And this is the javascript:
function editProfile(){
$('#editModal').modal();
var attributeList1 = ["selectedProfileName","profileMnemonic", "txDataRate", "rxDataRate", ... more attributes ...];
for(var i=0;i<attributeList1.length;i++)
{
attr1=attributeList1[i];
if (attr1 == "txDataRate" || attr1 == "rxDataRate"){
if (profileData[attr1]==1){
document.getElementById(attr1).selectedIndex=0;
}
else if (profileData[attr1]==2){
document.getElementById(attr1).selectedIndex=1;
}
else if (profileData[attr1]==3){
document.getElementById(attr1).selectedIndex=2;
}
else if (profileData[attr1]==6){
document.getElementById(attr1).selectedIndex=3;
}
else if (profileData[attr1]==5){
document.getElementById(attr1).selectedIndex=4;
}
else {
document.getElementById(attr1).selectedIndex=5;
}
}
else{
document.getElementById(attr1).value=profileData[attr1];
}
}
}
Edited to add: I have tried $('#'+attr1+' option:[value='+profileData[attr1]+']').prop('selected',true);
to select the correct dropdown option with no change.
(Found that suggestion here)
Screenshot of problem showing the filled textboxes and empty dropdowns.
SOLVED! Another function was overwriting the html ID's and thus my if/for statements were not able to change the selected dropdown ID.