-3

i want to know why this error is occuring "Uncaught TypeError: Cannot read property 'value' of null" and where i am going wrong?

 function create()
        {
            var  textbox=document.createElement("input");
            var para=document.createElement("p");
            para.setAttribute("id","p");
            textbox.type='text';
            textbox.value='asdf';
            textbox.setAttribute("id","textbox");
                           textbox.setAttribute("onblur",document.getElementById('p').innerHTML=document.getElementById('textbox').value);
            document.body.appendChild(textbox);
        }
8
  • 1
    How should we know? Please see css-tricks.com/reduced-test-cases. Commented Jan 7, 2015 at 19:29
  • It means you are trying to get a property named value from a variable that is null. Commented Jan 7, 2015 at 19:30
  • 1
    Sorry but could this title be anymore generic? Commented Jan 7, 2015 at 19:30
  • 1
    possible duplicate of Why does jQuery or a DOM method such as getElementById not find the element? -- Though I think the misunderstandings present in this question are slightly different from the misunderstandings assumed in that canonical duplicate. Commented Jan 7, 2015 at 19:30
  • 1
    The downvotes are a bit excessive. The question is ok, just the title is bad. Commented Jan 7, 2015 at 19:39

5 Answers 5

5

Before you've added an element to the DOM, you can't search for it with .getElementById(). That call returns null, and that's what the error is telling you.

It's pointless to do that anyway, since you've already got variables that refer directly to your newly-created elements. edit oh wait, I see what you're trying to do.

First, there's no reason to use .setAttribute() here. Just set properties directly on the DOM nodes you've created. You have to set the "onblur" property to a function:

function create() {
            var  textbox=document.createElement("input");
            var para=document.createElement("p");
            para.id = "p";
            textbox.type='text';
            textbox.value='asdf';
            textbox.id = "textbox";
            textbox.onblur = function() {
              para.innerHTML = textbox.value;
            };
            document.body.appendChild(textbox);
            document.body.appendChild(para);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

I added a line to append the <p> you created to, because if you don't do that there's no point to any of it.
2

I'll try to tackle some of the problems with your code (in hope that they magically match your HTML):

function create(){
    var textbox = document.createElement("input");
    var para = document.createElement("p");
    para.setAttribute("id", "p");
    textbox.type = 'text';
    textbox.value='asdf';
    textbox.setAttribute("id","textbox");
    // The following is a valid way to attach an event handler:
    textbox.onblur = function(){ 
        para.innerHTML = document.getElementById('textbox').value);
    }
    document.body.appendChild(textbox);
}

Comments

1

I've encountered a similar problem before:

"Uncaught TypeError: Cannot read property 'value' of null"

Means that you're probably didn't set an id in order for the function to look for the value, so it can retrieve it.

Example:

<input type='text' />
<div onclick='myfunc()'></div> 
function myfunc() {
   text = document.getElementById('msg').value;
}

This will return the following error you get. Correct approach:

<input type='text' id='msg'/>
<div onclick='myfunc()'></div>      
function myfunc() {
     text = document.getElementById('msg').value;
    }

Hope that helps!

Comments

1

ok i was facing the same problem do one thing,the script tab for your javascript file put it at the bottom of the html so that any input in html page comes, can be recognized by the js file ,because javascript will come after the value you want to play with

Comments

0

You should modify the function like so, for it to work. If you want to use setAttribute then you will have to wrap it in a function for the onBlur event

 function create() {
     var textbox = document.createElement("input");
     var para = document.createElement("p");
     para.setAttribute("id", "p");
     textbox.type = 'text';
     textbox.value = 'asdf';
     textbox.setAttribute("id", "textbox");
     textbox.setAttribute("onblur", function() {
          para.innerHTML = textbox.value;
     });
     document.body.appendChild(textbox);
 }

Or you can checkout the jsfiddle, to see how it works.

It was recommended to check in a previous answer, if the element is null, but in this case it's not needed as you are creating all the elements in the functions, so all elements will exist.

Also what do you plan to do with the paragraph element? Maybe wrap the input in the paragraph?

If you want you can add the paragraph simply by adding

document.body.appendChild(para);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.