views:

395

answers:

4

I have 2 chekboxes on a page. There are wrapped in a table cell each within their own row. Doing a document.getElementById('chk1_FEAS~1005') returns the element but document.getElementById('chk5_STG2~1005') is null. For what reasons could this be happening? (I'm testing in IE 8).

<input id="chk1_FEAS~1005" value="JobStages###StageCode~JobCode###FEAS~1005" onclick="addRemoveRow(this.value,this.checked)" style="border-width:0px;padding:1px;margin:0px;height:14px;"  type="checkbox" />

<input id="chk5_STG2~1005" value="JobStages###StageCode~JobCode###STG2~1005" onclick="addRemoveRow(this.value,this.checked)" style="border-width:0px;padding:1px;margin:0px;height:14px;"  type="checkbox" />
+1  A: 
document.getElementById('hk5_STG2~1005')

should be

document.getElementById('chk5_STG2~1005')

:-)

Wahnfrieden
+9  A: 

Your Id has invalid characters:

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 (".").

More information here.

CMS
+1 this is the only correct response
cletus
This is not the issue with the example, although you are correct that that is the recommendation.
Mitchel Sellers
Ok fair enough. But why would the first one work?
cletus is right. The tilda causes javascript to ignore the remaining part of the Id (depending on browser), so it basically thinks both elements have the same ID: "chk1_FEAS", which is why one works, and one does not.
IPX Ares
@IPX Ares: they don't both begin with chk1_FEAS though, so no.
Wahnfrieden
and that is why I need more coffee... maybe it is just looking at the 1005? I know I have seen this before with invalid characters.
IPX Ares
I'm tempted to think this is IE behaving funny. I can give those new id's so im happy to do that. Aside, any of you guys know how to get all the checkbox elements beginning with chk? $("input[type='checkbox'][id*=chk]") isn't working for me?
A: 

Looking at your sample it is a typo, the second item should be, document.getElementById('chk5_STG2~1005')

Also, I would recommend removing the ~ character as it is invalid for an id.

Mitchel Sellers
yep sorry it was a typo. document.getElementById('chk5_STG2~1005') is not working
A: 

For what it's worth, I have similar problems using document.getElementById on checkboxes in IE8. Try adding this tag to your head section :-

<meta http-equiv="X-UA-Compatible" content="IE=7" />

This will force IE8 into IE7 compatibility mode. It works for me as a workaround until I find-out what's really up.

MikeJ-UK