On 2 Apr 2004 06:32:21 -0800, Manu Ashok <ma********@yahoo.com> wrote:
this particular piece of code is not working in netscape.
That's because you're using IE syntax and proprietary Microsoft features.
function getList(listfrom, listto){
var currSelect = document.forms["actionmaster"].elements[listfrom];
var strid = "";
for (i = 0; i < currSelect.options.length; i++)
{
if (strid.length > 0)
{
strid = strid + "," + currSelect.options(i).innerText;
}
else
{
strid = currSelect.options(i).innerText;
}
}
document.forms["actionmaster"].elements[listto].value = strid;
return (strid == '' ? false : true);
}
How do i make this browser compatible..........?
I would re-write it like this:
function getList( listFrom, listTo )
{
var form = document.forms[ 'actionmaster' ];
var select = form.elements[ listFrom ];
var t = '';
for( var i = 0, n = select.length; i < n; ++i )
{
if( t.length ) // t is not zero-length
{
t += ',' + select.options[ i ].text;
}
else
{
t = select.options[ i ].text;
}
}
form.elements[ listTo ].value = t;
return t.length;
}
Notice that you should use brackets, not parentheses, to subscript a
collection (options is a collection property, not a method), and that the
HTMLOptionElement.text property will return the text between the opening
and closing OPTION tags, and that innerText isn't needed at all.
Finally, I know that "return t.length" will return a number. However, if
it is zero, it will evaluate to boolean false, or true otherwise. This
means you can write:
if( getList(...) ) {
// t was not an empty string
} else {
// t was an empty string
}
If you would rather return a boolean value from the function, use either
return !!t.length;
or
return( '' != t );
Using the conditional tertiary operator is overkill for such a simple
conversion.
Mike
In future, please don't indent your code from the left margin so much: it
causes extensive wrapping. Also, use two or four spaces for block
indentation, not tabs.
--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)