myk247365 Posted December 12, 2007 Share Posted December 12, 2007 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 More sharing options...
myk247365 Posted December 12, 2007 Author Share Posted December 12, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.