最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError is Null - Stack Overflow

programmeradmin1浏览0评论

I'm fixing a simple user input mulptiplication table which has been littered with errors. I'm stuck with a partucular piece of code and do not understand what it means.

When the script is run in firbug it says "TypeError: document.getElementById(...) is null"

This is the code which is attached to html:

var get = function(name){return document.getElementById("name").value;};

var set = function(name,value){document.getElementById("name").value=value;};

I'm fixing a simple user input mulptiplication table which has been littered with errors. I'm stuck with a partucular piece of code and do not understand what it means.

When the script is run in firbug it says "TypeError: document.getElementById(...) is null"

This is the code which is attached to html:

var get = function(name){return document.getElementById("name").value;};

var set = function(name,value){document.getElementById("name").value=value;};
Share Improve this question edited Dec 21, 2013 at 15:54 Rahul Tripathi 173k33 gold badges291 silver badges339 bronze badges asked Dec 21, 2013 at 15:52 RD5KRD5K 151 gold badge1 silver badge6 bronze badges 2
  • 1 Are you sure is DOM loaded when you call getElementById? – Sean Doe Commented Dec 21, 2013 at 15:55
  • Thanks guys I think I understand now what I am doing wrong. – RD5K Commented Dec 21, 2013 at 16:09
Add a ment  | 

3 Answers 3

Reset to default 2

That simply means that there is no element with an id property of "name" in the DOM. Perhaps your code runs before the document is ready?

It looks like this code is meant to query the DOM for an element with the id of name:

var get = function(name){return document.getElementById(name).value;};
var set = function(name,value){document.getElementById(name).value=value;};

It means that the element with id = "name" is not found in document. Look in the dom if it exists. You can also try to add this code as event handler for ready-event to check that it works ok there - as it is already said maybe you run this code before the dom is loaded.

发布评论

评论列表(0)

  1. 暂无评论