So i am learning javascript and i am checking out js reactiong to my html button and this does not seem to work it always says "cannot read properties of undefined(reading ‘log’)"
Here’s my code:
Html:
`<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<label>name: </label>
<input type= text id ="mytext">
<button type=button id= "mybutton"> submit</button>
<body>
<script src = index.js></script>
</body>
</html>`
Js:
let name = undefined;
document.getElementById("mybutton").onclick = function(){
let name = document.getElementById("mytext")
}
console.log(name);
3
Answers
The solution is sth like that:
Your code has two simple bugs, or wrong usages.
.value
afterdocument.getElementById("mytext")
console.console.log(name)
butconsole.log(name)
here is an example:
html code:
javascript code:
use call the console log inside the function. also you have duplicated console
console.console.log(name);
you should useconsole.log(name);
here is the jsfiddle test : https://jsfiddle.net/qxucho84/