tags:

views:

607

answers:

6

I started a thread here http://stackoverflow.com/questions/1266470/document-getelementbyid-not-working but it looks as though even the suggestions made were valid I still have a problem.

I have a couple of checkboxes. When i view the page source here there are. document.getElementById('chk1') is the only one that is not null. How could that be?

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

<input id="chk1" 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="chk2" value="JobStages###StageCode~JobCode###N/C~1005" onclick="addRemoveRow(this.value,this.checked)" style="border-width:0px;padding:1px;margin:0px;height:14px;"  type="checkbox" />

EDIT: Pretty much the complete code

<tr id='rw1' onMouseOver='setHover(this,event);' class='DRAW~1005      '
    onClick='setClick(this,event);'>
    <td id='RowId_0' width=0 style='display: none'>1</td>

    <td id='PrimaryKey_0' width=0 style='display: none'>DRAW~1005</td>
    <td id='StageCode_0' width=0 style='display: none'>DRAW</td>
    <td id='Allocated_0' nowrap
     style='overflow: hidden; height: 16px; width: 136px; overflow: hidden; text-align: center; padding-left: 5px;'
     class='col1'><input id="chk0"
     value="JobStages###StageCode~JobCode###DRAW~1005"
     onclick="addRemoveRow(this.value,this.checked)"
     style="border-width: 0px; padding: 1px; margin: 0px; height: 14px;"
     type="checkbox" /></td>
    <td id='StageCode_0' nowrap
     style='overflow: hidden; height: 16px; width: 136px; overflow: hidden; text-align: left; padding-left: 5px;'
     class='col2'></td>
    <td id='StageDescription_0' nowrap
     style='overflow: hidden; height: 16px; width: 123px; overflow: hidden; text-align: left; padding-left: 5px;'
     class='col3'
     onclick="showTextEditor(event,'JobStages','StageDescription','StageCode~JobCode','DRAW~1005      ');">
    </td>
    <td id='StartDate_0' nowrap
     style='overflow: hidden; height: 16px; width: 171px; overflow: hidden; text-align: left; padding-left: 5px;'
     class='col4'
     onclick="showCalendar(event,'JobStages','StartDate','StageCode~JobCode','DRAW~1005      ');">
    </td>
    <td id='EndDate_0' nowrap
     style='overflow: hidden; height: 16px; width: 112px; overflow: hidden; text-align: left; padding-left: 5px;'
     class='col5'
     onclick="showCalendar(event,'JobStages','EndDate','StageCode~JobCode','DRAW~1005      ');">
    </td>
</tr>

<tr id='rw2' onMouseOver='setHover(this,event);' class='FEAS~1005      '
    onClick='setClick(this,event);'>
    <td id='RowId_1' width=0 style='display: none'>2</td>

    <td id='PrimaryKey_1' width=0 style='display: none'>FEAS~1005</td>
    <td id='StageCode_1' width=0 style='display: none'>FEAS</td>
    <td id='Allocated_1' nowrap
     style='overflow: hidden; height: 16px; width: 136px; overflow: hidden; text-align: center; padding-left: 5px;'
     class='col1'><input id="chk1"
     value="JobStages###StageCode~JobCode###FEAS~1005"
     onclick="addRemoveRow(this.value,this.checked)"
     style="border-width: 0px; padding: 1px; margin: 0px; height: 14px;"
     type="checkbox" /></td>
    <td id='StageCode_1' nowrap
     style='overflow: hidden; height: 16px; width: 136px; overflow: hidden; text-align: left; padding-left: 5px;'
     class='col2'></td>
    <td id='StageDescription_1' nowrap
     style='overflow: hidden; height: 16px; width: 123px; overflow: hidden; text-align: left; padding-left: 5px;'
     class='col3'
     onclick="showTextEditor(event,'JobStages','StageDescription','StageCode~JobCode','FEAS~1005      ');">
    </td>
    <td id='StartDate_1' nowrap
     style='overflow: hidden; height: 16px; width: 171px; overflow: hidden; text-align: left; padding-left: 5px;'
     class='col4'
     onclick="showCalendar(event,'JobStages','StartDate','StageCode~JobCode','FEAS~1005      ');">
    </td>
    <td id='EndDate_1' nowrap
     style='overflow: hidden; height: 16px; width: 112px; overflow: hidden; text-align: left; padding-left: 5px;'
     class='col5'
     onclick="showCalendar(event,'JobStages','EndDate','StageCode~JobCode','FEAS~1005      ');">
    </td>
</tr>
A: 

just a hunch, but maybe ie8 doesnt like it when the input dont have a name (especially since they are checkboxes)

Niko
-1, name is a deprecated attribute
Jason
It doesn't matter that it's deprecated if the browser he's testing still uses it. IE6 for example could have trouble.
krdluzni
Name is only deprecated for the elements a, applet, form, frame, iframe, img, and map. Not deprecated for use with the input tag.
David
@Jason what are you talking about? name isn't deprecated for form elements.
Peter Bailey
....however, name isn't what's breaking here, folks.
Jason
@Jason: IE has broken for much, much less. Try not adding an href to a link and see the many ways IE6 will complain. It was a valid suggestion. Folk.
Triptych
+8  A: 

You are probably executing the javascript before the DOM nodes are available.

this will not work

<script type="text/javascript">
  // trying to retrieve #chk1 before it exists in the DOM!
  document.getElementById('chk1');
</script>

<input id="chk1">

Whereas either of these will

<input id="chk1">

<script type="text/javascript">
  // #chk1 exists in DOM, this works
  document.getElementById('chk1');
</script>

or

<script type="text/javascript">
  // Force execution to wait until window is done i.e, DOM is ready
  window.onload = function()
  {
    document.getElementById('chk1');
  }
</script>

<input id="chk1">

EDIT

I copied your entire chunk of HTML from above, added this to the end of it

<script type="text/javascript">
alert([ 
    document.getElementById('chk0')
  , document.getElementById('chk1')
]);
</script>

and opened it in IE7. The alert produced [object],[object] which means it worked and found both nodes. Not sure what to tell you.

Peter Bailey
Not possible I don't think. As I stated in the example I can access the middle checkbox (chk1) so if what you were saying was true I would not be able to access any of them. This is a v strange problem to me
Tested your code, couldn't reproduce the error.
Peter Bailey
I know that Peter I tried that myself. On the face of it everything looks ok. I guess i should have phrased my question something along the lines of , what can cause a document.getElementById to fail , e.g. invalid characters in the id etc
A: 

Are you using the .checked property of the checkbox to see if it is checked or not? Take a look at this example. It explains how you may evaluate a checkbox's check state.

http://www.developertutorials.com/tutorials/javascript/controlling-checkboxes-javascript-050629/page1.html

gsirianni
I can't access the element so therefore cannot access the checked property
A: 

Are you using chk0 and chk2 as the element id for any other html element on the page.

Rajat
There could be one more reason.I was seeing your complete code.May be due to special characters the dom elements containing chk0/chk2 might not be parsed at all.Instead of doing document.getElementById.Can you run thru all the elements of the form holding the checkboxes and see if chk0 and chk2 are listed there or not.
Rajat
chk0 and chk2 are not element ids for any other elements. No other elements begin with chk apart from these checkboxes furthermore. Rajat, which special characters?
A: 

I agree with the other answer, you are probably trying to access the dom before its loaded. Try this

window.onload = function(){alert(document.getElementById('chk1'));}

See how that works for ya

Zoidberg
I'm trying to access the element in a js function which is a handler to a button click event. I'm only pressing this once the page is fully loaded. Nothing else is called before button onclick event
A: 

With Jquery, try this:

<script type="text/javascript">
  $().ready(function() {
    $('#chk1');
  });
</script>

It will execute once the DOM is ready to be manipulated, this should fix your issue.

marcgg
ive not played with jquery too much but i do have it referenced. I'm getting object does not support this property. Is that syntax correct ?
I just tested it and yes, it's correct. Make sure you reference jquery correctly
marcgg