0

I am new to programming and I have a small problem

I have a form named "fr" with an input text box named "in" and a variable "n" with the value of "my text" this is my code what I have:

<html>
<head>
<script LANGUAGE="JavaScript">
var n = "my text";
document.fr.in.value = n;
</script>
</head>

<body>
<form name="fr">
<input name="in" size="3">
</form>
</body>
</html>

but somehow input "in" does not show the text "my text" I have been browsing the internet but I couldn't find any solution which works.. everything what I try does not work. I think I am doing something very simple wrong. please help me.

0

3 Answers 3

3

document.fr does not exist yet at time of invocation; hence, everything following it doesn't exist either, so it throws a TypeError

TypeError: Cannot read property 'in' of undefined

To fix this, move your code to be invoked after the nodes exist, using your favourite method

window.addEventListener('load', function () {
    var n = "my text";
    document.fr.in.value = n;
});

I'll further note that;

  1. The preferred way to look up an Element is to give it an id attribute and use document.getElementById. An id must be unique.
  2. Using the language attribute of <script> is depreciated, if you want to specify the language, use the type attribute type="text/javascript" or type="application/javascript"
  3. Opening the Console when a script is not working as expected will often show you the cause immediately. This is usually done with F12.
Sign up to request clarification or add additional context in comments.

Comments

0

You should init the script after the form is defined, as explained by Paul S. in his answer. So you may do,

<html>
<head>
</head>

<body>
<form name="fr">
<input name="in" size="3">
</form>

<script type="text/javascript">
var n = "my text";
document.forms.fr.in.value = n;
</script>
</body>
</html>

This would run the script after the form is defined. Or put this code in some function, and instantiate the function after the form is defined(i.e. loaded).

1 Comment

This will still throw the same TypeError
0

As Paul pointed out you should only try to get a hold of page elements when you are certain that the element you are interested has already been loaded. So in this case you can set the value of the input field by running your code when the page has fully loaded and by getting a reference to the input like this:

window.addEventListener("load", function () {
    var n = "my text";
    var myInput = document.getElementsByName("in");
    myInput[0].value = n;
});

Note, because getElementsByName() returns an array, you will have to use [0], to get the first element.

1 Comment

TypeError: Cannot set property 'value' of undefined

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.