View Full Version : getElementById() and the <label> element

02-20-2006, 04:20 PM
Hi all,

I am trying to write a simple script which will dynamically change the background colour of a <label> element, but for some strange reason I get an error in IE when using getElementById() in conjunction with <label>.

I can't work out what is happening, as I can use getElementById() with any other HTML element except the <label> element. The error I get in IE is "Object doesn't support this property or method".

I've searched all over the web for someone with a similar problem but I can't find anything, so please reply if you can help!

My code is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<script type="text/javascript">

window.onload = init;

function init() {

strawberry = document.getElementById('strawberry');
banana = document.getElementById('banana');

banana.style.background = '#f00';





<label for="strawberry" id="banana">dfdfd</label>
<input type="text" name="strawberry" id="stawberry" />