How can I change the CHANGE ME text inside my script

Viewed 82

I've tried this code on my pc and it doesn't work, does anyone know why?

There is this paragraph that contains the text CHANGE ME. It contains an id of "warning", now I want to change that text when someone clicks on the login button. It does not work on my pc. What am I doing wrong?

    <!DOCTYPE html>
    <html style="height: 100%; width: 100%; margin: 0;">
    <head>
        <title></title>
    </head>
    
    <body style="display: flex; height: 100%; width: 100%; margin: 0;">
    <!-- maxlength="20"> -->
    
    <form style="margin: auto; border: solid; padding: 10px;">
        
        <label style="font-size: 40px;"> Welcome! </label>
        <br>
        <input id="passwordText" type="password" placeholder="Password" maxlength="20">
        <input id="loginButton" type="button" value="Login" onclick="submit()">
        <br>
        <p id="warning">CHANGE ME</p>
    
    </form>
    
    <script>
    
        function toggleWarningText(text)
        {
            document.getElementById('warning').innerHTML = text;
        }
    
        function submit()
        {
            //toggleWarningText("");

            let password = document.GetElementById("passwordText").value;

            //This togglewarningtext doesn't work
            toggleWarningText(password);
    
            //This below doesn't work either
            if(password.length < 4)
            {
                toggleWarningText("The password must be at least 4 characters long");
            } 
            else 
            {
                toggleWarningText(password);
            }    
        }
    
    </script>
    
    </body>
    
    </html>

2 Answers

I think you're looking for

function toggleWarningText(text)
    {
        document.getElementById('warning').value= text;
    }

instead of .innerHTML

Change submit() function name. It calls form submit() function.

  <!DOCTYPE html>
    <html style="height: 100%; width: 100%; margin: 0;">
    <head>
        <title></title>
    </head>
    
    <body style="display: flex; height: 100%; width: 100%; margin: 0;">
    <!-- maxlength="20"> -->
    
    <form style="margin: auto; border: solid; padding: 10px;">
        
        <label style="font-size: 40px;"> Welcome! </label>
        <br>
        <input id="passwordText" type="password" placeholder="Password" maxlength="20">
        <input id="loginButton" type="button" value="Login" onclick="clicked()">
        <br>
        <p id="warning">CHANGE ME</p>
    
    </form>
    
    <script>
    
        function toggleWarningText(text)
        {
            document.getElementById('warning').innerHTML = text;
        }
    
        function clicked()
        {
            //toggleWarningText("");

            let password = document.getElementById("passwordText").value;

            //This togglewarningtext doesn't work
            toggleWarningText(password);
    
            //This below doesn't work either
            if(password.length < 4)
            {
                toggleWarningText("The password must be at least 4 characters long");
            } 
            else 
            {
                toggleWarningText(password);
            }    
        }
    
    </script>
    
    </body>
    
    </html>

Related