1

I have two questions.
1) I wrote this html tag and created javascript in separate file.

<input type="text" id="callno" value="3333">

In the js, I have

var PhoneNumber = document.getElementById("callno").value;

However, when I look at the console in the browser (Google Chrome) I saw that there is an error Uncaught TypeError: Cannot read property 'value' of null.

2) How can I make that each time when I update value of html tag in above the value of PhoneNumber updated automatically.

TrojanByAccident
  • 217
  • 6
  • 20
  • have you included a – Tom O. Dec 21 '16 at 19:05
  • yes. The error only here and defined other paramteres too. But they work well. –  Dec 21 '16 at 19:06
  • could you post the entire html and external script? – Tom O. Dec 21 '16 at 19:07
  • The error message itself indicates that the problem is not in getting the value, but getting the element (it's trying to read the property 'value' of an object, but the object it's trying to read from is null - it isn't selecting the element). So that's the first place to start. Based on that, the first question to ask is: where is your Javascript? Is it before or after the page structure? Is it inside a window.load or $(document).ready function? If the JavaScript is being called before the page is rendered, it won't wait to execute. – CGriffin Dec 21 '16 at 19:08
  • 1
    Please post each question separately. – TrojanByAccident Dec 21 '16 at 19:10
  • Post your complete code of HTML. btw for #2 you can use `onchange` or `onkeyup` or `onkeydown` event. – Ashok Shah Dec 21 '16 at 19:13
  • Yes I fixed that thank you so much –  Dec 21 '16 at 19:14

2 Answers2

1

You are most-likely attempting to run that JavaScript code BEFORE the HTML element has been parsed into the DOM.

When the JavaScript runs, the input element hasn't been loaded into memory yet, so your document.getElementById() returns null and then you attempt to get the value of that (hence your error message).

You can either move your script to the end of the body element or you can set up an event handler for when the page is loaded.

Scenario #1 (Move the script so that it occurs AFTER all HTML)

<html>
<head>
</head>
<body>
<input type="text" id="callno" value="3333">
<script>
(function(){
  // It's best to just cache DOM refrences, not actual properties
  // so that you can reuse the reference for multiple reasons
  
  // Don't do this:
  //var phoneNumber = document.getElementById("callno").value;
  
  // Do this:
  var phoneElement = document.getElementById("callno");
  
  // We'll declare a variable outside of the callback function
  // that will hold the actual value later.
  // This way, you can use the variable at any point you may need to. 
  var phoneNumber = null
  
  // Now, we can do many things with that element.
  // In this case, we'll wire it up to an input event
  // handling function that fires whenever the element
  // receives input
  phoneElement.addEventListener("input", function(){
    // And then we can use it to get its stored value
    // It's important that we update the variable when
    // the textbox value changes.
    phoneNumber = phoneElement.value
    console.log(phoneNumber);
  });
}());
</script>
</body>
</html>

Scenario #2 (Set up an event handler that waits until the page is ready)

<html>
<head>
  <script>
    // Wait until the window has parsed all the HTML elements
    // before running its callback function:
    window.addEventListener("DOMContentLoaded", function(){
      
      // This function won't execute until all the HTML elements
      // have been read into memory. By now, the input element
      // we need is ready to be read.
      
      // It's best to just cache DOM refrences, not actual properties
      // so that you can reuse the reference for multiple reasons
  
      // Don't do this:
      //var phoneNumber = document.getElementById("callno").value;
  
      // Do this:
      var phoneElement = document.getElementById("callno");
  
      // We'll declare a variable that will hold the actual value
      // This way, you can use the variable at any point you may need to. 
      // If we only set the value here though, it won't be updated when
      // the value changes.
      var phoneNumber = phoneElement.value;
  
      // Now, we can do many things with that element.
      // In this case, we'll wire it up to an input event
      // handling function that fires whenever the element
      // receives input
      phoneElement.addEventListener("input", function(){
        // And then we can use it to get its stored value
        // It's important that we update the variable when
        // the textbox value changes.
        phoneNumber = phoneElement.value
        console.log(phoneNumber);
      });
    });
</script>
</head>
<body>
 <input type="text" id="callno" value="3333">
</body>
</html>
Scott Marcus
  • 57,085
  • 6
  • 34
  • 54
  • Yes you were right. Thank you so much. And could youe please help me how I automatically assign value of textbox when I make a change that. –  Dec 21 '16 at 19:14
  • I will mark but it said I should need to wait 2 minutes more ))))) –  Dec 21 '16 at 19:15
  • A down vote for the correct answer, an explanation and two working examples of how to solve the problem?! Seriously?! That is NOT how SO works people! – Scott Marcus Dec 21 '16 at 19:23
  • I already voted to you. –  Dec 21 '16 at 19:24
  • Yes, thank you. But someone else down voted.. :( – Scott Marcus Dec 21 '16 at 19:26
  • Scott Could you please explain this part? –  Dec 21 '16 at 19:29
  • Scott Could you please explain this part? phoneElement.addEventListener("input", function(){ // And then we can use it to get its stored value console.log(phoneElement.value); }); –  Dec 21 '16 at 19:30
  • It's pretty well explained in the comments. `addEventListener` sets up a function to be called when the element in question experiences the specified event. In this case the element is the textbox and the event is `input`, which fires every time the textbox receives any input. In that funciton, we are getting the value of the textbox and logging it to the console. – Scott Marcus Dec 21 '16 at 19:39
  • @MamedShahmarov I have adjusted the answers to show how the phone number can be stored in a variable so that it can be used in other places. – Scott Marcus Dec 21 '16 at 19:45
  • This is the better answer than the selected, but I also think it is important to keep in mind that the person who asked the question is new and this isn't the most simple solution to the problem. – Thomas Juranek Dec 21 '16 at 19:55
  • @ThomasJuranek Suggesting a solution that doesn't explain the problem, uses well-known bad practices and doesn't explain why the solution works is no solution at all. It does a dis-service to the OP and anyone else who may read it because they don't know any better. You are just reinforcing bad habits. An answer with noting more than "This should work" should not be the accepted answer. – Scott Marcus Dec 21 '16 at 19:57
  • Thanks for the downvote, and even though you are right it is the choice of the person who asked the question. – Thomas Juranek Dec 21 '16 at 20:22
  • @ThomasJuranek I didn't down vote you. But, I'm not surprised that your answer got down voted for all the reasons I stated. – Scott Marcus Dec 21 '16 at 20:41
-1

This should work:

<!DOCTYPE html>
<html>
<body>

<input id="myInput" onchange="myFunction()" value="1234">

<p>Change the above input value and click enter.</p>


<p id="myOutput">VALUE HERE</p>

<script>

var x;

function myFunction() {
    x = document.getElementById("myInput").value;
    document.getElementById("myOutput").innerHTML = x;
}
</script>

</body>
</html>
Thomas Juranek
  • 333
  • 2
  • 15
  • Thomas thank you so much. I want to know xhow can I assign this value to variable instead of document.getElementById("myOutput").innerHTML = x; –  Dec 21 '16 at 19:34
  • Could you explain that differently? – Thomas Juranek Dec 21 '16 at 19:35
  • The diffirence is that I want to use that variable in other place. But what I understood from here that the value here will be as part of html if I understood correctly. I am sorry if I make a mistake. Because I am fresh in software development –  Dec 21 '16 at 19:39
  • I edited my code snippet. By putting X outside of the function, it can be accessed anywhere in the script, not just inside the function. But the value will still update as wanted whenever the input value changes. If it helps I'd appreciate an upvote. :) +Mamed Shahmarov – Thomas Juranek Dec 21 '16 at 19:41
  • 1
    Thank you so much –  Dec 21 '16 at 19:46
  • This is a very sloppy solution. Global variables should always be avoided as well as inline HTML event handlers. – Scott Marcus Dec 21 '16 at 19:52
  • Yes, it isn't a preferred solution, and could be made better: http://stackoverflow.com/questions/11218553/onchange-event-is-not-working-well. There are many solutions but this works well enough with his existing program. – Thomas Juranek Dec 21 '16 at 19:53
  • 1
    I wouldn't say that suggesting the use of anti-patterns to new coders is working "well enough". – Scott Marcus Dec 21 '16 at 19:55