Document.getElementById (still) not working

I started the topic here document.getElementById does not work , but it looks like even the suggestions made were valid. I still have a problem.

I have a couple of flags. When I browse the source of the page here. document.getElementById ('chk1') is the only one that is not null. How could this 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: Extremely 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>
+3
source share
8 answers

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

it will not work

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

<input id="chk1">

While any of them will be

<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 the entire HTML fragment on top, added it to the end

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

IE7. [object],[object], , . , .

+10

, , : .getElementById type=text. type=text , , ... window_onload, , --.

, , .getElementsByName . , , . , .

+7

, , , dom .

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

, ya

+1

, , , ie8 , (, )

0

chk0 chk2 html .

0
source

Using jQuery, try the following:

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

It will be executed after the DOM is ready for manipulation, this should fix your problem.

0
source

Also, if you run this in your facebook application and use the 'f' prefix, for example f0 or f_10, then it doesn’t work either, I found that I found that I would share, since this is what I was looking for in google for

0
source

Source: https://habr.com/ru/post/1714987/


All Articles