HighDots Forums  

Re: how to define javascript workable across browsers particularly innertext

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: how to define javascript workable across browsers particularly innertext in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Winter
 
Posts: n/a

Default Re: how to define javascript workable across browsers particularly innertext - 04-02-2004 , 09:59 AM






On 2 Apr 2004 06:32:21 -0800, Manu Ashok <manu_ashok (AT) yahoo (DOT) com> wrote:

Quote:
this particular piece of code is not working in netscape.
That's because you're using IE syntax and proprietary Microsoft features.

Quote:
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.Winter (AT) blueyonder (DOT) co.invalid (replace ".invalid" with ".uk" to reply)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.