I am having a main form and having button in the main form , when i click the button i am calling another form, there i am having ok button.my below mentione djava script is in that ok button.
i am having a listbox field in my second form, i want to get multivalue from this field, but my below code gets only first value from selected list box . how to get all selected values. pls advice me.
var pf = opener.document.forms[0];
var mf = document.forms[0];
var s,s1, ss;
var i = mf.fslct.options.selectedIndex;
if(i != -1)
{
alert(mf.fslct.options.length);
for(var j=0; j<mf.fslct.options.length; j++)
{
if(mf.fslct.options[j].selected == true)
{
s = mf.fslct[mf.fslct.selectedIndex].text;
s1 = pf.flg1.value;
eval(“window.opener.document.forms[0].”+s1+“.value = s;”);
}
}
pf.flg1.value = “”;
self.close();
}
else
{
alert(‘Please select a name first’)
return false;
}
Subject: Multivalue in java script
Try something like (untested):
var pf = opener.document.forms[0];
var mf = document.forms[0];
var s,s1, ss;
var i = mf.fslct.options.selectedIndex;
if(i != -1)
{
alert(mf.fslct.options.length);
for(var j=0; j<mf.fslct.options.length; j++)
{
if(mf.fslct.options[j].selected == true)
{
s += (s ? ';' : '') + mf.fslct.options[j].text;
}
}
s1 = pf.flg1.value;
eval("window.opener.document.forms[0]."+s1+".value = s;");
pf.flg1.value = "";
self.close();
}
else
{
alert('Please select a name first')
return false;
}
Basically, you have to loop through the options testing the ‘selected’ property like you already do, but instead of just getting the selectedIndex (of which there is only ever one) you have to build up a string by adding the current option to ‘s’.
I’ve seperated the selected values with a ‘;’ but it’s entirely up to you and the needs of your ap how you do that.
The ‘(s ? ‘;’ : ‘’)’ bit is so that we don’t get a ‘;’ in there before the first value or if there’s only 1 value selected.