Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.log is clearing out immediately and I don't know why

I am trying to test this small feature and I believe I wrote everything correctly, however it does not show up in my console.log. Is there something I am over looking?

          var inputChange = function() {
           var inputVal = document.getElementById("input").value;
            console.log(inputVal)
          };
 form{
                width: 300px;
                height: 75px;
                position: absolute;
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
                margin: auto;
            }
    
    </head>
    
    <body>
    
    
    <form>
      <input id="input" type="text" name = "input" placeholder="Type Something"/>
      <input type="submit" onclick ="inputChange()"/>
    </form>
    
      
    </body>
    </html>
like image 628
jehicks2 Avatar asked Sep 01 '25 02:09

jehicks2


1 Answers

HTML inputs of type submit send their form whenever you click on them, so the script is getting called, and you're seeing the output briefly, but then the page is getting reloaded and the console is getting cleared. Change

<input type="submit" onclick ="inputChange()"/>

to

<input type="button" onclick ="inputChange()"/>

and you should be good.

like image 80
ddsnowboard Avatar answered Sep 02 '25 17:09

ddsnowboard