0

I'm just trying to output a value, but whenever it gets to "var max = tb1.value;" it stops working. I can't find out what's wrong with my code.

<html>
<head>
<script language="javascript" type="text/javascript">

var tb1 = document.getElementById('textbox1');
	
function output() {
    var max = tb1.value;
    document.write( max );
}

</script>
<title>asd</title>
</head>

<body>

<form>
<input name="max" type="text" id="textbox1"><br>
<input name="submit" type="button" value="Enter" onClick="output()">
</form>

</body>
</html>
4
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by six_sic6
0

Hi,

Please move the line of code "var tb1 = document.getElementById('textbox1');" inside the function output(). Please find the modified code below.

Thanks
Aravind

<html>
<head>
<script language="javascript" type="text/javascript">


	
function output() 
{
	var tb1 = document.getElementById('textbox1');
    var max = tb1.value;
    document.write( max );
}

</script>
<title>asd</title>
</head>

<body>

<form>
<input name="max" type="text" id="textbox1"><br>
<input name="submit" type="button" value="Enter" onClick="output()">
</form>

</body>
</html>
0

Hi,

Please move the line of code "var tb1 = document.getElementById('textbox1');" inside the function output(). Please find the modified code below.

Thank you, that works. Is there any way to use it as a global variable though? I want to use it in more than just that one function.

0

You can make it better by passing in an element ID instead of using global variable.

<html>
<head>
<script type="text/javascript">
// do not need langauge="" in the script tag because it is outdated
function output(elementID) {
  var tb1 = document.getElementById(elementID);
  if (tb1) {  // ensure that the element exists or javascript will stop working
    var max = tb1.value;
    document.write( max );
  }
}

</script>
<title>asd</title>
</head>

<body>

<form>
<input name="max" type="text" id="textbox1"><br>
<input name="submit" type="button" value="Enter" onClick="output('textbox1')">
</form>

</body>
</html>
0

Dont use the var tb1. Just use document.getElementById('textbox1').value anywhere in your javascript block

0

Using only document.getElementById('textbox1').value without checking whether or not the element exists could cause your JavaScript to stop working right where the error occurs (when the element 'textbox1' does not exist). You could, however, use document.getElementById('textbox1') to test, but it will make your code to be longer and more difficult later on when you want to manipulate the same element again and again. Declaring a local variable to point to the element would not cost much and is easier to manipulate & more intuitive.

Edited by Taywin: n/a

0

Using only document.getElementById('textbox1').value without checking whether or not the element exists could cause your JavaScript to stop working right where the error occurs (when the element 'textbox1' does not exist). You could, however, use document.getElementById('textbox1') to test, but it will make your code to be longer and more difficult later on when you want to manipulate the same element again and again. Declaring a local variable to point to the element would not cost much and is easier to manipulate & more intuitive.

thanks for the advice. It will help me in future.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.