html:
<body>
<input type="text" id="input" placeholder="Search for...">
<button>Click</button>
</body>
js:
const input = document.querySelector('#input');
const inputValue = input.value;
const button = document.querySelector('button');
button.addEventListener('click',()=>{
console.log(inputValue);
});
const inputValue = document.querySelector('#input').value;
const button = document.querySelector('button');
button.addEventListener('click',()=>{
console.log(inputValue);
});
I don’t get it, why can’t I get value using above two ways? Value keeps showing empty in console but with below code, I can…why??
const input = document.querySelector('#input');
const button = document.querySelector('button');
button.addEventListener('click',()=>{
console.log(input.value);
});