Click to See Complete Forum and Search --> : another 'almost'


LAwebTek
02-05-2003, 10:08 PM
The following code works fine for removing options from a select list, except that if I remove the last option in the list I want the option that becomes the new-last option (options.length) to become selected. Any ideas?

function remove() {
playList = document.amp.playlist.options;
for(var i=0; i<playList.length; i++) {
if(playList[i].selected && playList[i] != "") {
playList[i].value = "";
playList[i].text = "";
}
}
BumpUp();
}

function BumpUp() {
for(var i = 0; i < playList.length; i++) {
if(playList[i].value == "") {
for(var j = i; j < playList.length - 1; j++) {
playList[j].value = playList[j + 1].value;
playList[j].text = playList[j + 1].text;
}
var ln = i;
break;
}
}
if(ln < playList.length) {
playList.length -= 1;
}
}

khalidali63
02-05-2003, 10:22 PM
just add the line below to where you want to set the last option to be selected

playList [playList.length-1].selected=true;

cheers

Khalid

LAwebTek
02-05-2003, 10:37 PM
I suppose I'll have to split the points on this one (don't spend them all in one place). khali forgot to mention the conditional statement or where the code should go, but I figured it out. Thanks!!

if(ln < playList.length) {
playList.length -= 1;
if (ln == playList.length) playList[playList.length - 1].selected = true;
}

LAwebTek
02-05-2003, 11:04 PM
correction - should look like this:

function remove() {
var playList = document.amp.playlist.options;
var i, j, len = playList.length;
for(i=0; i < len; i++) {
if(playList[i].selected && playList[i] != "") {
playList[i].value = "";
playList[i].text = "";
}
}
BumpUp();
}

function BumpUp() {
var playList = document.amp.playlist.options;
var i, j, len = playList.length;
for(i = 0; i < len; i++) {
if(playList[i].value == "") {
for(j = i; j < len - 1; j++) {
playList[j].value = playList[j + 1].value;
playList[j].text = playList[j + 1].text;
}
var ln = i;
break;
}
}
if(ln < len) {
playList.length -= 1;
if (ln == playList.length && ln != 0) {
playList[playList.length - 1].selected = true;
}
}
}