Just for a change... this doesn't work in IE .. but does in Mozilla!

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

    Just for a change... this doesn't work in IE .. but does in Mozilla!

    Hi,

    Code below dynamically adds an input box to a form, creating a new name
    attribute as it goes... (many thanks to Martin Honnen for getting me this
    far ;0) Problem is - in IE5.5 only the first form field (the hard-coded one)
    is recovered from the form, while with Mozilla/Firefox all fields are.

    The alert() just shows that the newly created element has no name in IE. Can
    anyone offer help with this bizarre problem (I thought any old rubbish would
    run in IE - even mine!)

    Regards
    Rae MacLeman
    ----------------------------------------------------------------------------
    ---------

    <HTML>
    <HEAD>
    </HEAD>
    <SCRIPT LANG=Javascript >
    <!--
    var gCount=1;
    function DoChange() {
    var objNewName, objBR;
    gCount++;
    objNewName = document.create Element('INPUT' );
    objNewName.setA ttribute('NAME' , 'fname'+gCount) ;
    alert('object name = '+objNewName.na me);
    objNewName.setA ttribute('ID', 'name'+gCount);
    objBR = document.create Element('BR');
    document.getEle mentById("names ").appendChild( objNewName);
    document.getEle mentById("names ").appendChild( objBR);
    delete objNewName;
    delete objBR;
    }
    //-->
    </SCRIPT>

    <BODY>
    <form action="mozilla _test2_handler. asp" method="POST">
    <div id="names" name="names">
    <input id="fname1" name="fname1" type="text"><br >
    </div>
    <INPUT TYPE=button ONCLICK=DoChang e() VALUE='Add name'><input
    type="submit"></form>
    </BODY>
    </HTML>


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.775 / Virus Database: 522 - Release Date: 10/8/04


  • Michael Winter

    #2
    Re: Just for a change... this doesn't work in IE .. but does in Mozilla!

    On Sun, 10 Oct 2004 07:50:21 +0100, webdev <someone@micros ofdt.com> wrote:

    [snip]
    [color=blue]
    > <HEAD>[/color]

    Valid documents require a TITLE element to be present.
    [color=blue]
    > </HEAD>
    > <SCRIPT LANG=Javascript >[/color]

    There is no such thing as a lang attribute. You intended language, but
    even that should be avoided as it has been deprecated in favour of the
    (required) type attribute.

    <script type="text/javascript">
    [color=blue]
    > <!--[/color]

    Hiding scripts is an obsolete practice. All browsers in use understand the
    SCRIPT element, even if they can't execute scripts.
    [color=blue]
    > var gCount=1;
    > function DoChange() {
    > var objNewName, objBR;
    > gCount++;
    > objNewName = document.create Element('INPUT' );
    > objNewName.setA ttribute('NAME' , 'fname'+gCount) ;
    > alert('object name = '+objNewName.na me);
    > objNewName.setA ttribute('ID', 'name'+gCount);[/color]

    Don't use the setAttribute method with HTML documents. Instead, use the
    relevant properties.

    objNewName.name = 'fname' + gCount;
    objNewName.id = 'name' + gCount;
    [color=blue]
    > objBR = document.create Element('BR');
    > document.getEle mentById("names ").appendChild( objNewName);
    > document.getEle mentById("names ").appendChild( objBR);[/color]

    Instead of calling getElementById twice, consider saving the reference
    returned by the first call and use it twice.
    [color=blue]
    > delete objNewName;
    > delete objBR;[/color]

    You cannot delete local variables. It won't cause an error, but it doesn't
    do anything, either. You can see this by looking at the return value of
    the delete operator. If the property being deleted doesn't exist, or it
    does and has been deleted, the delete operator returns true. If the
    property cannot be deleted, the operator returns false.

    // In a function
    var test = {};
    alert(test); // alerts [object Object]
    alert(delete test); // alerts false
    alert(test); // alerts [object Object]

    Even if you could delete a local variable, there's no point. When the
    function returns, local variables are destroyed[1] and any objects will
    have their reference counts decremented.

    [snip]
    [color=blue]
    > <div id="names" name="names">[/color]

    DIV elements do not have a name attribute.

    [snip]
    [color=blue]
    > <INPUT TYPE=button ONCLICK=DoChang e() VALUE='Add name'>[/color]

    You need to quote that onclick attribute value.

    ... onclick="DoChan ge()" ...

    [snip]

    Hope that helps,
    Mike


    [1] Obviously, a closure will stop this, but that doesn't occur here. If
    it did, assign null to the variables to explicitly remove the references.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • webdev

      #3
      Re: Just for a change... this doesn't work in IE .. but does in Mozilla!


      "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
      news:opsfnewutt x13kvk@atlantis ...[color=blue]
      > On Sun, 10 Oct 2004 07:50:21 +0100, webdev <someone@micros ofdt.com> wrote:
      >
      > [snip]
      >[color=green]
      > > <HEAD>[/color]
      >
      > Valid documents require a TITLE element to be present.
      >[color=green]
      > > </HEAD>[/color][/color]
      [SHORTENED}

      Many thanks for that.

      Rae MacLeman




      ---
      Outgoing mail is certified Virus Free.
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.775 / Virus Database: 522 - Release Date: 10/8/04


      Comment

      • Michael Winter

        #4
        [Clarificaton] Re: Just for a change... this doesn't work in IE .. but does in Mozilla!

        On Sun, 10 Oct 2004 09:48:31 GMT, Michael Winter
        <M.Winter@bluey onder.co.invali d> wrote:
        [color=blue]
        > On Sun, 10 Oct 2004 07:50:21 +0100, webdev <someone@micros ofdt.com>
        > wrote:[/color]

        [snip]
        [color=blue][color=green]
        >> <SCRIPT LANG=Javascript >[/color]
        >
        > There is no such thing as a lang attribute.[/color]

        Before anyone comments on that potentially misleading statement, I'll
        rephrase it.

        The lang attribute does exist as part of the internationalis ation (i18n)
        attribute set, however it is not applicable to SCRIPT elements. It
        specifies a two character language code[1], along with another optional
        subset code separated by a dash (-), specifying the language used as
        content in the relevant element.

        For example, to indicate that an entire document is written in English,
        you can specify:

        <html lang="en">

        Whereas

        <img lang="fr" alt="Voici Fabienne, mon &eacute;pous e."
        src="fabienne.j pg">

        indicates that the alt text is French.

        [snip]

        Mike


        [1] Language codes specified in ISO 639, and the accompanying subset
        codes, are usually two characters. However, they can be anything from one
        to eight characters in length. Some registered examples of this include
        i-klingon and en-scouse.

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        Working...