Jump to content

options.add() removes option


myk247365

Recommended Posts

I have two select objects and want to move selected options from one select(pm) to another(cm). The add() function works to add the option to cm, but it also removes the option from pm. This causes the options object to shrink and as the code loops, the current index moves beyond the end of the options array. Any items near the end of the list are skipped.here's the code:function add_opt(){ var t=get_tid(); var cm=document.getElementById('cmsel'+t); var pm=document.getElementById('pmsel'+t); for(o in pm.options){ if(pm.options[o].selected && !in_array(pm.options[o],cm.options)){ cm.add(pm.options[o],null); } }}I've thought about using a different type of loop, but I can quite wrap my head around how deal with an array that may or may not shrink during a pass of the loop.any suggestions?myk

Link to comment
Share on other sites

I was able to work out the following solution: for(o=0;o<l;o++){ if(pm.options[o].selected && !in_array(pm.options[o],cm.options)){ cm.add(pm.options[o],null); o=o-1; l=pm.options.length; } }hope this helps, any suggestions, please reply.myk

I have two select objects and want to move selected options from one select(pm) to another(cm). The add() function works to add the option to cm, but it also removes the option from pm. This causes the options object to shrink and as the code loops, the current index moves beyond the end of the options array. Any items near the end of the list are skipped.here's the code:function add_opt(){ var t=get_tid(); var cm=document.getElementById('cmsel'+t); var pm=document.getElementById('pmsel'+t); for(o in pm.options){ if(pm.options[o].selected && !in_array(pm.options[o],cm.options)){ cm.add(pm.options[o],null); } }}I've thought about using a different type of loop, but I can quite wrap my head around how deal with an array that may or may not shrink during a pass of the loop.any suggestions?myk
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...