how to define javascript workable across browsers particularly innertext

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manu Ashok

    #1

    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(listfro m, listto){
    var currSelect = document.forms["actionmast er"].elements[listfrom];
    var strid = "";
    for (i = 0; i < currSelect.opti ons.length; i++)
    {
    if (strid.length > 0)
    {
    strid = strid + "," + currSelect.opti ons(i).innerTex t;
    }
    else
    {
    strid = currSelect.opti ons(i).innerTex t;
    }
    }
    document.forms["actionmast er"].elements[listto].value = strid;
    return (strid == '' ? false : true);

    }

    How do i make this browser compatible..... .....?

    thanx in anticipation

    manu
  • Michael Winter

    #2
    Re: how to define javascript workable across browsers particularly innertext

    On 2 Apr 2004 06:32:21 -0800, Manu Ashok <manu_ashok@yah oo.com> wrote:
    [color=blue]
    > this particular piece of code is not working in netscape.[/color]

    That's because you're using IE syntax and proprietary Microsoft features.
    [color=blue]
    > function getList(listfro m, listto){
    > var currSelect = document.forms["actionmast er"].elements[listfrom];
    > var strid = "";
    > for (i = 0; i < currSelect.opti ons.length; i++)
    > {
    > if (strid.length > 0)
    > {
    > strid = strid + "," + currSelect.opti ons(i).innerTex t;
    > }
    > else
    > {
    > strid = currSelect.opti ons(i).innerTex t;
    > }
    > }
    > document.forms["actionmast er"].elements[listto].value = strid;
    > return (strid == '' ? false : true);
    > }
    >
    > How do i make this browser compatible..... .....?[/color]

    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
    HTMLOptionEleme nt.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@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    Working...