While using JS arrow functions, I noticed something very strange that if the name of the constant the arrow function is assigned to exactly matches the “name=” for an input field that the arrow function is selecting with querySelector, the “___ is not a function” error occurs if calling the arrow function’s constant from an onchange.
I’m sure I’m not smart enough to have found a JS bug, so:
Why is a “___ is not a function error” returned by the onchange if the constant name and “name=” field match exactly??
Am I somehow accidentally passing in the constant itself into the query selector? I feel like it’s a hardcoded string not a constant name.
Example:
Updating the “firstName” input will say not a function, but the “lastName” input does not.
Interestingly the not a function is only reported as not a function by the onchange, but if calling firstName() from the console works fine. JS was added in the head using defer.
In the example below, entering text into the firstName box and hitting tab will cause the onchange to fail as it doesn’t recognize the function, but entering text into the lastName box and hitting tab works fine, as the constant is called “notLastName” instead of “lastName”.. and I don’t understand why the first fails and the 2nd works..
A code snippet can be run below. Ignore the CSS.
“firstName()” directly into the console does work.
const firstName = () => document.querySelector('[name="firstName"]').value;
const notLastName = () => document.querySelector('[name="lastName"]').value;
/* 1. RESETS
==================================
Used to to normalize things like box-sizing.
*/
*,
*:before,
*:after {
box-sizing: inherit;
box-sizing: border-box;
}
/* 2. VARIABLES
==================================
The baseline styling for html elements.
*/
:root{
--standardPadding: 8px 16px;
}
/* 3. HTML ELEMENTS
==================================
The baseline styling for html elements.
*/
html {
height: 100%;
max-height: 100%;
overflow: hidden;
padding: 0;
}
body {
background: linear-gradient(338deg, rgba(206,210,218,1) 16%, rgba(255,255,255,1) 100%);;
height: 100%;
max-height: 100%;
margin: 0;
display: flex;
flex-direction: column;
color: #1a1a1a;
}
nav {
background: radial-gradient( circle farthest-corner at 10% 20%, rgba(37,145,251,0.98) 0.1%, rgba(0,7,128,1) 99.8% );
color: rgba(255,255,255);
font-size: 1em;
font-family: Arial;
padding: 4px 16px;
}
nav h1 {
margin: 0;
font-weight: normal;
}
form {
display: flex;
width: 300px;
max-width: 100%;
overflow: auto;
flex-direction: column;
background-color: white;
padding: 20px;
margin: 16px auto auto auto;
border-radius: 6px;
border-style: solid;
border-width: 1px;
border-color: rgba(0,0,0,0.1);
}
form label:last-of-type {
margin-bottom: 0;
}
label {
font-family: Nunito;
color: #1a1a1a;
margin-top: 4px;
z-index: 1;
padding-left: 8px;
color: rgba(0,0,0,0.5);
}
label + input {
margin-top: -24px;
}
input {
font-family: Nunito;
height: 54px;
margin-bottom: 16px;
border: solid 1px#ced4da;
border-radius: .25 rem;
padding: 32px 8px 16px 8px;
font-size: 1rem;
border-radius: 4px;
/* padding: 16px 8px 8px 8px; */
}
input:focus{
outline-color: dodgerblue;
}
input:active{
outline-color: dodgerblue;
}
footer {
margin-top: 16px;
padding: 4px 16px;
font-family: Nunito;
background-color: white;
}
/* 4. CUSTOM
==================================
Custom classes
*/
.invalid{
outline: solid 2px darkred;
border: none;
}
.valid{
outline: solid 2px #2A9D8F;
border: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Input Test</title>
</head>
<body>
<nav>
<h1>
Input Test: First Box Fails After "tab", 2nd Works
</h1>
</nav>
<form>
<label for="FirstName">First Name</label>
<input type="text" name="firstName" value="" onchange='alert("First Name:" + firstName())''>
<label for="lastName">Last Name</label>
<input type="text" name="lastName" value="" onchange='alert("Last Name:" + notLastName())''>
</form>
</body>
</html>


