Small Javascript Question

Discussion in 'Javascript' started by Mtek, May 12, 2008.

  1. Mtek

    Mtek Guest

    Hi,

    I have a form which puts out a hidden DIV like this:


    <DIV id='invalid' style="display:none"><img src="../images/
    flyer.jpg"></DIV>

    So, the DIV is hidden until the customer performs some actions. Once
    they do, I make the DIV appear like this:

    document.getElementsByName('custphoto')[0].style.display="";

    That works fine in IE. But Firefox gives this error:

    Error: document.getElementsByName("custphoto")[0] has no properties

    Why does that not work in Firefox, but does in IE? Can I make it
    work in both browsers???

    Thank you!
     
    Mtek, May 12, 2008
    #1
    1. Advertising

  2. Mtek

    RobG Guest

    On May 12, 12:02 pm, Mtek <> wrote:
    > Hi,
    >
    > I have a form which puts out a hidden DIV like this:
    >
    > <DIV id='invalid' style="display:none"><img src="../images/
    > flyer.jpg"></DIV>
    >
    > So, the DIV is hidden until the customer performs some actions. Once
    > they do, I make the DIV appear like this:
    >
    > document.getElementsByName('custphoto')[0].style.display="";


    Presumably that is a typo and you mean 'invalid' not 'custphoto'.


    > That works fine in IE. But Firefox gives this error:
    >
    > Error: document.getElementsByName("custphoto")[0] has no properties
    >
    > Why does that not work in Firefox, but does in IE?


    Two good reasons:

    1. IE does not understand that name and ID attributes are
    actually different attributes

    2. the div doesn't have a name attribute, it has an ID.
    getElementsByName should get elements with the same name
    attribute value, not elements with the same ID attribute
    value

    and one more for good measure:

    3. A Div element in an HTML document is not specified as
    having a name attribute, therefore you should not expect
    getElementsByName (which belongs to the DOM HTML spec) to work.


    > Can I make it work in both browsers???


    You can make it work in any browser that supports DOM 1 (which is
    pretty much any browser released in the last 8 years or so) by using
    getElementById:

    document.getElementById('invalid').style.display = '';


    --
    Rob
     
    RobG, May 12, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig G

    small javascript function problem

    Craig G, Mar 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    382
    MasterGaurav
    Mar 3, 2005
  2. Arthur Connor
    Replies:
    0
    Views:
    385
    Arthur Connor
    Aug 27, 2003
  3. Mike
    Replies:
    1
    Views:
    111
    Thomas 'PointedEars' Lahn
    Aug 21, 2004
  4. lawrence

    IE problem with small bit of Javascript

    lawrence, Dec 1, 2004, in forum: Javascript
    Replies:
    3
    Views:
    108
  5. Amir
    Replies:
    2
    Views:
    126
Loading...

Share This Page