I have a very basic question that I thought I knew but it's driving me crazy. Help, please!!!
I'll tell you, it's simple...
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<script>
document.f1.f1t1.value = 30
</script>
<title>Document</title>
</head>
<body>
<form name="f1" id="f1">
<input type="text" name="f1t1" id="f1t1">
</form>
</body>
</html>
I want to print the number 30 inside the text field from JavaScript, as the default and start value. I understand that this is the correct syntax because I access the form field following its DOM hierarchy and indicate that its value is 30.
Well, what happens to me is that on the screen my text field loads empty and the Google Chrome inspector tells me that "Uncaught TypeError: Cannot read property 'f1t1' of undefined". It is as if it does not recognize the object "f1" and also as if it interprets "f1t1" as a property of "f1".
Well, there I leave the doubt to see if someone gives me a cable.
Thank you very much.
If you use jquery, you can do like this:
HTML
JAVASCRIPT/jQuery
"$" refers to jQuery; then you can search for the element with "("#f1t1")" and then assign the value with ".val('30');"
I understand what you want to do, although there are other more elaborate ways to do it.
In order for it to work the way you want it to, you have to place the script in the body , after your form tag .
In the following way:
In this way, your script will be executed when the controls (form and textbox) are already loaded in the DOM.
If you want to see the working code I leave it in JSBIN: http://jsbin.com/zafaset/edit?html,console,output
You still need to tell Js when you want it to print.
When the DOM Loads, the function "X" is created and js is told to run that function on startup with window.onload = X;