dimanche 5 septembre 2021

I have an if else statement that I dont know how to make the else part relevent

const newInput = document.querySelector('input');
const h1 = document.querySelector('h1');



newInput.addEventListener('change', function(){

    if (newInput != ''){
     h1.innerText = 'Welcome ' + newInput.value
    } else {
    h1.innerText = 'Enter Your Username';
    }
});
<head>
    <title>Input Event</title>
    <!--LEAVE THESE LINES ALONE, PLEASE! THEY MAKE THE LIVE PREVIEW WORK!-->
    <!-- <script src="node_modules/babel-polyfill/dist/polyfill.js" type="text/javascript"> </script> -->
    <!-- <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> -->


</head>

<body>
    <h1>Enter Your Username</h1>
    <input type="text" id="username">

    <script src="InputEvent.js"></script>
</body>

</html>

My if else statement appears to be ignored unless I change it to (if (newInput === 'a value' ) at which point it stops the function.

I don't understand why I can make the if statement relevant but it wont continue to the else part of the code.

Can anyone shed some light?

Aucun commentaire:

Enregistrer un commentaire