how to define javascript workable across browsers particularly innertext

M

Manu Ashok

hi
this particular piece of code is not working in netscape.
pls help me


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..........?

thanx in anticipation

manu
 
M

Michael Winter

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top