- Want to check first that, Is text box contains nothing on page load ?
Then assign some value to it.
if (document.getElementById("txtBoxID").value == null) { document.getElementById("txtBoxID").value = "Some text here"; }
But getting error "JavaScript runtime error: Unable to set property 'value' of undefined or null reference" How to do this?

- 626
- 1
- 6
- 28
-
Did you solve your problem? – mortb Jun 29 '13 at 08:10
7 Answers
Are you doing your code above after the page is loaded?
window.onload = function() {
if (document.getElementById("txtBoxID").value == null) {
document.getElementById("txtBoxID").value = "Some text here";
}
}
Note that window.onload
isn't best way to check whether page is loaded. It depends on the browser's spec. See window.addEventListener or window.attachEvent or IE.
It is because your DOM is not ready so wait until load complete:
window.onload=function(){
if (document.getElementById("txtBoxID").value == null) {
document.getElementById("txtBoxID").value = "Some text here";
}
};

- 28,160
- 11
- 74
- 110
You're probably trying to access the element before you defined in page, so you should execute that code on window.load
event or move that code just before </body>
as alternative you may use the placeholder
attribute supported on many modern browsers
<input type="text" id="txtBoxId" placeholder="Some text here" />
If the input has no value, the placeholder value will be shown instead
(polyfills of this behaviour are available for older browser)

- 120,726
- 26
- 164
- 177
You are trying to access the property 'value' on a null (or undefined) object, wich cause an exception, assuming your ID may not be txtBoxID

- 2,103
- 1
- 16
- 34
It is not safe to test null for the textbox value. You should use empty string instead. I created a jsFiddle which shows this:
<input type="text" id="txtBx" />
document.getElementById("txtBx").value = null;
alert("test 1 textbox is null \n" + (document.getElementById("txtBx").value == null));
alert("test 2 textbox is empty string \n" + (document.getElementById("txtBx").value == ""));
(I'm using Chrome)

- 9,361
- 3
- 26
- 44
-
using the falsy nature, `if (!document.getElementById("txtBoxID").value)` might suffice. This would account null, "" and 0 (which might be a problem). – Christoph Jun 28 '13 at 12:45
Please try the below code, you need to set the value to the null first in the line
Name: <input type="text" id="myText" value=''>
Here is the demo :
function func() {
if (document.getElementById("myText").value == '') {
document.getElementById("myText").value = "Some text here";
} else {
alert("not null");
}
}
Name: <input type="text" id="myText" value="">
<p>Click the button to change the value of the text field.</p>
<input type="button" id="button" value="Click Me" onClick="func();" />
If you have JQuery available here is another way of accomplishing what you are asking for. In the document.ready function you can get the value of your text using the id of your textbox and wrapping it in JQuery. This will allow you to check and replace the value of your text box in the case that it is empty by using the .val() function
$(document).ready(function () {
if($('#id-of-your-textbox').val() == ' ')
{
$('#id-of-your-textbox').val('some text');
}
});