View Full Version : setting background color in Javascript doesn't make this color appear under IE7

07-07-2011, 12:36 PM

I'm faced with a problem trying to set background color under IE7. I have the following Javascript:

function showLayer793BKColor(id)
var txtObj = document.all(id);
if (txtObj.style.display == 'none')
txtObj.style.display = ''
txtObj.style.backgroundColor = 'grey';

function hideLayer793BKColor(id)
var txtObj = document.all(id);
if ( txtObj.style.backgroundColor == 'grey' )
txtObj.style.display = 'none';

These functions are used to show or hide div blocks.
These blocks are, for example, specified in the following way:

<div id="l_gct5tekst" style="display:none">
<b>GCT 5. Eerste verkenning problematiek</b>

and, for example,

<div id="l_Keuze" style="display:none">
<b>GCT 5</b>

The whole configuration works smoothly when using IE8. However, when using IE7 I get an error msg like "Invalid value for property". When I use the Color propert iso the BackgroundColor property I get no error anymore but of course I don't have a background color anymore then.

In what way can I specify the use of a background color under IE7 ? Or is just not possible in one way or the other.

Furthermore, what more differences between JS under IE7 and IE8 do I have to take into account ?

Do I also have to rewrite my div block in some way (using some attribs ?) to cope with IE7 ?

Thanks in advance,

Diederick van Elst

07-07-2011, 01:42 PM
First of all you should switch from document.all() to document.getElementById() which is the standards compliant way of addressing elements by their id property.

Second: Can you show us where and how you call the specified functions?

07-08-2011, 11:42 AM
On the other hand the id token must not start with a digit. See also the W3C Recommendation:

"ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")."

07-08-2011, 12:47 PM
It's a lower case L and not a 1 (one)

07-08-2011, 12:51 PM
use getElementById()

var txtObj = document.getElementById(id);

document.all is an old, IE only, reference. Even so, you use it incorrectly:

var txtObj = document.all(id);

document.all[] needs square bracket notation, not rounded squares. This is the reason for IE7 throws that error.

07-11-2011, 05:37 PM
Thanks for the comments given.

I'm making adjustments in very old legacy code (ca. 8 years old). I understand now that one shouldn't use document.all[] anymore since the introduction of IE5.

However, I have to draw your attention again to my original question:

"In what way can I specify the use of a background color under IE7 ? Or is just not possible in one way or the other ?"

Like I said before, the code runs smoothly under IE8. This is regardless of document.all() issues.
The browser clearly indicates that the lines of code where the errors originate are the ones where the backgroundColor property is used.

For completeness I have posted the complete xsl file. I call the function showLayer793BKColor() by writing

function zet_aan_uit_5()
if( (document.invoermodel.Punt1Keuze.checked)
|| (document.invoermodel.Punt2Keuze.checked)
|| (document.invoermodel.Punt3Keuze.checked)
|| (document.invoermodel.Punt4Keuze.checked)
|| (document.invoermodel.Punt5Keuze.checked)
|| (document.invoermodel.Punt6Keuze.checked)
if( (document.invoermodel.Punt1Keuze.checked) )

In which 'l_Keuze' and 'l_Punt1Keuze' are id's of div blocks.

I would have liked to post the complete code but text length restrictions won't let me..