setting style and class dynamically

R

Richard Trahan

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?
 
M

Michael Winter

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top