Forum Moderators: open

Message Too Old, No Replies

drop down box javascript help

if the Q is silly still answer please...

         

content_adj

7:18 pm on Jul 8, 2007 (gmt 0)

10+ Year Member



This is the situation:

the listbox onchange calls a javascript, which checks whether this selected value is already selected in the users previous history. if so, a confirm dialog is triggered and asks

"hey you already added this. do you want to add it again?"

if the user selects yes then that particular options' selected = 'selected'

if the user selects no then that particular options' selected = ''

In the the below code, confirm box is showing but the selected is not changing.

javascript:

function showalert()
{

var id = document.getElementById('selection').selectedIndex;
var answer = confirm(hey you already added this. do you want to add it again?');
if (!answer)
id.selectedIndex.selected= "";
}

php:

<select id= "selection" name='name[]' onChange="showalert()" multiple ="multiple" size =5 >
<? for ($i = 0; $i < count($name); $i++) {?>
<option id="<? echo $i;?>" value="<? echo stripslashes($name[$i]);?>"> <? echo stripslashes($name[$i]);?></option> <? }?>
</select>

firefox error console says : id.selectedindex has no properties.

I am learning php now. I know very little javascript. pls help..

penders

1:15 pm on Jul 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



var id = document.getElementById('selection').selectedIndex; 
var answer = confirm(hey you already added this. do you want to add it again?');
if (!answer)
id.selectedIndex.selected= "";

Having a quick gander at your code it looks like you've been a bit overzealous with the .selectedIndex since you have already assigned the selectedIndex to the id variable. What your last line effectively says is:

document.getElementById('selection').selectedIndex.selectedIndex.selected = "";

And I don't recall there being a .selected property - certainly not of the .selectedIndex which just returns a number (the index of the selected item - or first item in the case of a multiple select)

So I think you can get away with changing your last line to:

id = -1; // Select nothing

Or, possibly easier to understand, is to remove .selectedIndex from your initial statement:

var id = document.getElementById('selection'); 
var answer = confirm(hey you already added this. do you want to add it again?');
if (!answer)
id.selectedIndex = -1;

mehh

4:11 pm on Jul 9, 2007 (gmt 0)

10+ Year Member



confirm(hey you already added this. do you want to add it again?');

am i the only one who saw that:
confirm("hey you already added this. do you want to add it again?");