setting style and class dynamically

Discussion in 'Javascript' started by Richard Trahan, Sep 19, 2004.

  1. This code doesn't work:

    document.getElementById("myid").style.backgroundColor = "#abcdef"

    This should work in NN7.2 and IE6.0. What am I doing wrong?

    "myid" refers to <input type="text" id="myid">, which gladly obeys
    an internal style="background-color: #abcdef".

    Also, I can't find any mention of a class property in the DOM; IOW,
    document.getElementById("myid").class always crashes, yet class works in
    the <input> tag. Is it not possible to dynamically change the class of
    an element?
     
    Richard Trahan, Sep 19, 2004
    #1
    1. Advertising

  2. On Sun, 19 Sep 2004 19:56:12 GMT, Richard Trahan <>
    wrote:

    > This code doesn't work:
    >
    > document.getElementById("myid").style.backgroundColor = "#abcdef"


    When are you executing that code? If it's before the element, myid, has
    been parsed you should get an error stating that myid couldn't be found.
    If not, please show an example including HTML (preferably in a host web
    page).

    [snip]

    > Also, I can't find any mention of a class property in the DOM; IOW,
    > document.getElementById("myid").class always crashes, yet class works in
    > the <input> tag. Is it not possible to dynamically change the class of
    > an element?


    It is possible. You set the class using the className property. The DOM
    HTML Specification briefly mentions why this name change took place.

    Good luck,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Sep 19, 2004
    #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. Mark Rae
    Replies:
    12
    Views:
    9,544
    Mark Rae
    Jul 12, 2006
  2. dlo_olb
    Replies:
    2
    Views:
    2,361
    Fredrik Lundh
    Jun 25, 2003
  3. Ken Varn
    Replies:
    0
    Views:
    525
    Ken Varn
    Apr 26, 2004
  4. Michael Bierman
    Replies:
    21
    Views:
    247
    Thomas 'PointedEars' Lahn
    Apr 18, 2005
  5. Jayden
    Replies:
    9
    Views:
    313
    alex23
    Sep 27, 2012
Loading...

Share This Page