Reputation: 9225
<select class="dName" name="dName">
<option SELECTED value="#" DISABLED>========================</option>
<option id="0" value="amensah">Abbey-Mensah, Michael</option>
<option id="1" value="acharya">Acharya, Niraj</option>
<option id="2" value="achonu">Achonu, Geoffrey C.</option>
<option id="3" value="agustin">Agustin, Erie</option>
<option id="4" value="agyemang">Agyemang, Kuragu</option>
</select>
I am using the ID in my Javascript:
if (i[iVar] == $(".dName").find('option:selected').attr('id')) {
To pull which option was selected.
Now let's say I want to delete options #2 for the dName select, instead of changing the #3 to #2 as ID and #4 to #3 as ID, can I use an array? This might be easy but having a 100+ options would make it really time consuming
Is there anyway to change the ID in the dName select so no matter what it is used as an array? Something like:
<option id="[]" value="amensah">Abbey-Mensah, Michael</option>
So if I delete an option in the middle of the list, it will keep the numbering structure in order?
RESOLVED:
if (i[iVar] == $(".dName").find('option:selected').attr('id')) {
Has been changed to:
if (i[iVar] == "the value of selection option") {
AND
document.getElementById('first').innerHTML = phyList[$(".dName").find('option:selected').attr('id')].firstName;
Has been changed to:
document.getElementById('first').innerHTML = phyList[$(".dName").find('option:selected').index()-1].firstName; //.index()-1 because the first option is "=" which is not being used
So I can completely get rid of the ID and not worry about deleting an option statement.
Upvotes: 1
Views: 197
Reputation: 206028
You don't need IDs at all, whatever you delete (.remove() ?) in your select
element take the index
of the selected option
:
just for example:
$('select').on('change', function(){
alert( $(':selected').index() );
});
http://jsbin.com/akefaq/2/edit
Upvotes: 4