0

I am struggling to display the output in the console using .value input in javascript

let userName;
document.getElementById('button1').onclick = function() {
  userName = document.getElementById('name').value;
  console.log(userName);
}
<label>Enter your name: </label><br>
<input type="text" id="name"><br>
<button type="button" id="button1">Submit</button>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • @CertainPerformance the button is `type="button"` though so it shouldn't submit the form – Phil Jul 18 '22 at 03:43
  • @Phil Oh, good point. Christian, Can you make a MCVE so we can see the problem for ourselves? Can't reproduce it given the code you have – CertainPerformance Jul 18 '22 at 03:44
  • Please [edit] your question to provide a [minimal, **reproducible** example](https://stackoverflow.com/help/minimal-reproducible-example). The code in your question works so it's hard to understand how or why it doesn't work for you. Make sure you're checking your browser console for errors – Phil Jul 18 '22 at 03:45
  • @CertainPerformance the problem with Stack Snippets is it puts the JS after the HTML. This is most probably a dupe of [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/q/14028959/283366) – Phil Jul 18 '22 at 03:46

0 Answers0