When I run the following code, it doesn't call textContent because it tells me that 'myTitle' doesn't exist.
var miTitulo=document.querySelector("h1");
var miBoton=document.querySelector("button");
console.log(miTitulo);
function Bienvenida() {
var miNombre= prompt('ingrese su nombre');
localStorage.setItem('nombre', miNombre);
miTitulo.textContent='Bienvenido '+miNombre;
}
window.addEventListener('load',Bienvenida,false);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="PruebaEditable.js"></script>
</head>
<body style="background: gray">
<h1>Bienvenido a mi pagina, </h1>
<button>Cambiar usuario</button>
</body>
</html>
The problem is that you are supplying a non-existent parameter to the
.queryselector()
. It is recommended that you always point to the id or class of the element when usingqueryselector()
.Your code works if for example we add
id
to each HTML element and use it as a reference for myTitle and myButton . So:You can check for more information: valid examples of parameters used in queryselector()
I hope to be helpful. All the best!