467,895 Members | 1,366 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

how to define javascript workable across browsers particularly innertext

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
Jul 23 '05 #1
  • viewed: 1383
Share:
1 Reply
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)
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.