Solution 1 :

At the start of your passwordGenerator function:

var passwordLength = document.getElementById('num').value;

This element (<span id="num">8</span>) doesn’t have a value attribute, so your function will try to generate a password with a length equal to undefined.

You should get the value from your slider instead:

var passwordLength = document.getElementById('slider1').value;

You can also simplify your function sliderMove function:

function sliderMove() {
    document.getElementById('num').textContent = document.getElementById('slider1').value;
}

Problem :

Problem: My password generator will not work until I move my slider. However, I would like the value to default to 8. So if someone loads page, and clicks generate PW, a random pw of 8 would populate.

//generate a password function
function passwordGenerator () {

// Length of the password?
var passwordLength = document.getElementById('num').value;


// characters options for PW
const values = "[email protected]#$%^&*()";


// defining password

var password = "";


// creating a loop to choose password

for (var i = 1; i <= passwordLength; i++) {
    password = password + values.charAt(Math.floor(Math.random() * Math.floor(values.length -1)));
}
  
// adding the password to the content area
document.getElementById('display').value = password;
}

// adjust value when moving slider
function sliderMove(){
 document.getElementById('num').value = document.getElementById('slider1').value;
 document.getElementById('num').textContent = document.getElementById('num').value;
}

//copy to clipboard
function selectText() {
  const input = document.getElementById('display');
  input.focus();
  input.select();
  document.execCommand('copy')
}
.backgroundWhite {

    background-color: white;
    border: darkgray 2px solid;
    padding-bottom: 25px;
}


.backgroundGray {
    background-color: lightgray;
    width: 100%;
    height: 500%;
}

.passwordBox {

    width: 500px;
    height: 200px;
    text-align: center;
}


body {
    height: 100%;
    background-color: lightgray;
}

.headText {
    padding: 50px;
}

.buttonOnClick {
    margin: 20px;
}
.passGenButton {
    color: white;
    background-color: red;
    margin-right: 15%;
    height: 40px;
    border-radius: 12px;
}
.copyButton {
    margin-left: 15%;
    background-color: darkgray;
    color: white;
    height: 40px;
    border-radius: 12px;
}

textarea {
padding: 20px;
font-size: 19px;
color: #4f4f4f;
}

.titleClass {

    padding-top: 10px;
}


@media (max-width: 537px) {
    .passGenButton {
        color: white;
        background-color: red;
        margin-right: 1%;
        height: 40px;
        border-radius: 12px;
    }
    .copyButton {
        margin-left: 1%;
        background-color: darkgray;
        color: white;
        height: 40px;
        border-radius: 12px;
    }
    .passwordBox {
        width: 400px;
        height: 200px;
        text-align: center;
    }
    .backgroundWhite {

        background-color: white;
        border: darkgray 2px solid;
        padding-bottom: 25px;
        padding-left: 20px;
    }

} 
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
        integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="app.js"></script>
    <title>Random Password Generator</title>
</head>

<body>
    <div class="conatiner backgroundGray">
        <div class="row">
            <div class="col-12">
                <div class="topText">
                    <!-- Header -->
                    <h1 class="text-center text-dark headText">Password Generator</h1>
                </div>

                <div class="container">
                    <div class='row'>
                        <div class="col-lg-12 col-sm-12 text-center">
                            <div class="content backgroundWhite">
                                <!-- Sub Header -->
                                <h4 class="titleClass">Generate a Password</h4>
                                <br />



                                <!-- Slider -->
                                <div class="slidecontainer">
                                        <p>Select PW Length</p>
                                    <input id="slider1" type="range" min="8" max="128" value="8" onchange="sliderMove()"
                                        class="robClass">
                                    <span id="num">8</span>
                                </div>
                                <br />

                                <!-- Password Box -->
                                <textarea class="passwordBox" type="text" id="display"
                                    placeholder="Your Secure Password"></textarea>
                                <br />
                                <button onclick="passwordGenerator()" class="passGenButton buttonOnClick">Generate
                                    Password</button>
                                <button class="buttonOnClick copyButton" defaultValue="8" onclick="selectText()">Copy to
                                    clipboard</button>
                                <div id='length'></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


</body>

</html>

I have played with HTML and JS. I have tried to create a var num = 8;
no luck.

Does anyone have any ideas on how I can do this?

Comments

Comment posted by developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/…

if this is pure JS, check on: window.onload:

Comment posted by MDN link

You should probably look into

Comment posted by RobC

Great feedback! Thank you!

By

Leave a Reply

Your email address will not be published. Required fields are marked *