I am currently learning javascript. And I stumbled upon this example code which makes me very confused.
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>xxxxx</title>
<script type="text/javascript">
function a() {
let x = parseInt(Form1.val1.value);
let y = parseInt(Form1.val2.value);
alert("Sum \n" + (x + y));
}
</script>
</head>
<body text="red">
<h1> xxxxx </h1>
<form name="Form1">
<input type="text" name="val1"><br><br>
<input type="text" name="val2">
<br><br>
<input type="button" value="Calul" onclick="a()">
</form>
</body>
</html>
Regardless of its content, this is a working code. What I cannot understand is that why using directly Form1 (The "name" of the form) can work here. (However, when I do the same to other element such as <p>
, it doesn't work)
I always thought we need to get everything (With pure JS) by using document.getElementByID / ByClass etc. in order to find the element in the HTML to manipulate.
Thanks!