My site serves dynamic contents so the header and footer remains the same. In the footer is my JS script that has a few document.getElementById('ids').addEventListener('click',function(e)
.
How can I prevent the JS error TypeError: document.getElementById(...) is null
when the content with the element ID was not yet served? See sample JS code below:
<script>
// DN_1, 1st page Switch On/Off
document.getElementById('dn_1_yes-no').addEventListener('click',function(e){
var attrChk = document.getElementById('dn_1_yes-no');
if( this.checked){
document.getElementById('external_dn_1').style.display='block';
}else{
document.getElementById('external_dn_1').style.display='none';
}
});
// DN_2, 2nd page Switch On/Off
document.getElementById('dn_2_yes-no').addEventListener('click',function(e){
var attrChk = document.getElementById('dn_2_yes-no');
if( this.checked){
document.getElementById('external_dn_2').style.display='block';
}else{
document.getElementById('external_dn_2').style.display='none';
}
});
// more.....
</script>