textbox value cannot be change using javascript

时间:2016-02-03 03:55:50

标签: javascript html

 document.getElementById("name").value = data;

it shows Uncaught TypeError: Cannot set property 'value' of null. why is this happen?

the data have the value.

1 个答案:

答案 0 :(得分:0)

You're receiving that error because there doesn't exist an element if an ID of name when that piece of JavaScript is executing.

document.getElementById("name") is returning null since no element is found with the specified ID. You then attempt to access the property value of null, which doesn't exist.

What's most likely happening is your JavaScript code is executing before the element exists (move your script to the end of the body or inside a document ready function, or you mis-typed the ID.