PDA

View Full Version : getElementbyID Problem


Carl
10-13-2002, 02:12 AM
Very simple, but why wont this work?

document.form.getElementByID("bname").style="fontweight:bold; borderstyle:inset;";

Carl

jkd
10-13-2002, 02:48 AM
Three reasons:

1. It is getElementById, not getElementByID

2. Your CSS properties are incorrect. font-weight, not fontweight.

3. HTMLElement.prototype.style is a reference to a CSSStyleDeclaration, not the actual attribute. Use setAttribute('style', 'font-weight:bold; border-style:inset;')
to be correct. You'll find though that Internet Explorer is incompetant and cannot do something as simple as this... instead, for IE, you'll need something like:
style.cssText = 'font-weight:bold; border-style:inset;'

Carl
10-13-2002, 03:00 AM
Ok got everything fixed except, the getElementById to work.

I'm sure theres a problem with getElementByID.

Carl

ahosang
10-13-2002, 12:25 PM
Only document has access to the getElementById function.
document.getElementById("bname")
NOT
document.form.getElementById("bname")
id's should be unique within a document.

jkd
10-13-2002, 04:15 PM
http://www.webxpertz.net/forums/showthread.php3?s=&threadid=19672

You missed something important.

I said that IE required style.cssText, but it is still technically the incorrect way, and will not work in Mozilla. Use style.setAttribute() in an if clause so that it will still work in standards-compliant web browsers.

adios
10-13-2002, 09:24 PM
var el = document.getElementById('bname');
if (el) {
el.style.fontWeight = 'bold';
el.style.borderStyle = 'inset';
}

CSS != DOM....