0
Follow
4
View

getElementbyId does not work and returns null

a215555 注册会员
2023-01-26 08:02
document.getElementById()

return single element

document.getElementById

function img() {
  $('.first-img p').fadeIn(500);
  $('.first-img span').fadeIn(500);
};

setTimeout(firstimg, 10);

window.addEventListener('DOMContentLoaded', function() {
  var elements = document.getElementById('firstimgs');
  img_elements.addEventListener('load', (e) => {
    console.log(" load");
  });

  img_elements.src = img_elements.getAttribute("src");
});
diaorong55 注册会员
2023-01-26 08:02

I had experienced this issue. In my case, the function was misspelled. It does matter, if the name of a function starts with a capital letter. Or if all the letters are the same, like in the name of the prototype. If you use an editor with automatic language completion, can be easier. Like should be Code, which could support more programming languages.

But it is not so sure, that the error it is because of getElementById. You just see, getElementById doesn't work and this seems to be a cause. This is just the effect we can see, the error could be found elsewhere. And the script stops at the first encountered error without a warning.

Just see what you had changed last time, look at each line of code. Sometimes it is not so obvious, why a certain kind of error appears. If nothing could work, you can also use a try / catch error checking.

With kind regards, Adrian Brinas

About the Author

Question Info

Publish Time
2023-01-26 08:02
Update Time
2023-01-26 08:02