Project : Password Generator

This project is about how to build a random password generator.

What is required is to have a password generator that generates a password containing upperCase letters only, or a password containing lower case letters only, or a password containing numbers only, or a password containing symbols only like below depending on what checkbox is clicked.

Password generator

At the end we will also see how to generate a mixed password that contains uppercase letters and symbols, or numbers and lowerCase letters and so on…

Also, we want to be able to copy this password to paste it somewhere else, so we have to build this little copy/paste symbol below:

ontent -->
Copy/paste symbol

1) Step 1: HTML & CSS part

First we will create a <div></div> that

will contain all of our elements.

And we will give the <div></div> a clas

s name of “main-container“.

If we refresh the HTML page we will not see anything. That is because we did not give the <div></div> a width and a height and a background-color value.

Let’s go to the index.css file and set these properties for the <div></div>.

So in index.html we’ll have the code:

<div></div> added to the HTML

And in index.css file we’ll have the code:

Adding CSS to the <div></div>

So in the browser we’ll have:

<div></div> in the browser

Nothing fancy so far, so let’s add our elements to this <div></div>.

Now we’ll add a <h1></h1> with text “Password Generator”.

And we’ll add another <div></div> with class name “password“. It is in this <div></div> that the password generated will be displayed.

Also inside this <div></div> we’ll add an emoji of the copy/paste symbol, this way when clicked the password will be copied.

So the HTML code will be:

<div><div> password added to the HTML

To add an emoji in HTML all you have to do is to look for the Unicode of the emoji you want to add (You can check the following link: , Copy the code in the code column, remove the “U+” and replace it with “&#x” or “&#” in the index.html file.

So in the browser we have:

Elements displayed on the browser

If you have noticed, the <div></div> with the class name “password” does not appear on the screen. Actually, it is there, but we can’t see it because we did not give it a height and a width and a background-color.

Also, we want the copy/paste emoji to be at the right bottom of the <div></div>.

So let’s add some CSS to the elements:

So here we added a width of 80% and a height of 50px to the <div></div> with class name of “password“, and a background-color of white so that we can see it.

Also as we want to center this <div></div> in the middle of the main-container <div></div> we used the CSS property margin and gave it a value of 0 auto.

And finally to add a border to the <div></div> with class name of “password” we used border CSS property with an outset value as type of the border.

To position the emoji we set the position of the <span></span> element to relative position so that we can apply the CSS properties left and top and move the element easily.

We want the emoji to be at the end of the <div></div> so the left property is set to 87%, and for the emoji to be at the bottom of the <div></div> the top property is set to 30%.

Also we want the emoji to be a little bit bigger to be visible so we use the font-size property, because remember, an emoji is a text, and to change the size of a text in HTML we use the font-size property.

We can change the color of the emoji by adding the color property. So here the color is black.

For the <div></div> with class name of “main-container” we added also a border-radius value of 20px to make the borders of the <div></div> rounded and a white color so that all the text written inside the <div></div> is white.

So on the browser we’ll have:

CSS added to the elements

Now it is time to add the rest of the elements.

So, for the text and the checkbox all we have to do is to add a paragraph, and inside of this paragraph add the text we want and a checkbox.

We’ll also add a horizontal line to separate each paragraph using the <hr> tag.

At the end we’ll add a button to generate the password.

So the code is:

Adding the rest of the HTML code

Here we added the checkbox using <input/> tag and giving it the type value of checkbox.

So this is it for the HTML part.

On the browser this will look like:

Browser results

Now we want the checkbox of each paragraph to be positioned at the end of the the <div></div>. And we want all the checkboxes to be aligned vertically.

To do that we’ll use the display property in CSS and we’ll give it a value of flex to make all the elements of the paragraph in one row that we call the main row.

After that we’ll add justify-content property and give it the value of space-between. It is this property that will add space between the text inside the paragraph and the checkbox and make them aligned vertically. Because this property makes the items evenly distributed in a row.

We’ll add align-items property and give it a value of center.

We’ll add also a font-size property because we want the text to look a little bit bigger and a padding property to add some space inside the paragraphs.

For the <hr> tag we’ll give it a width of 90% as we don’t want it to occupy all the width of the <div></div>.

And finally for the button we’ll add:

  • a display property and give it a value of block so that we can center the button
  • A padding property to make some space inside the button.
  • A margin property to make some space between the button and the last paragraph.
  • A border-radius to make the corners of the button rounded.
  • A color to change the color of button text.
  • A font-size to make the button text larger.
  • A background-color to add a color tp the button background.

Also, we want the background-color of the button and the color of the button text to change once the user hovers over the button.

So the code will be:

CSS code for the rest of the elements

So on the browser we have:

Password Generator

And when we hover over the button the color of the button changes and becomes:

Button’s background color change

Now one last thing before we finish, it would be better if the password generator was centered in the middle of the browser.

To do that we will apply the display property to the body and give it a value of flex, and then give it a height.

If we want the Password generator to be centered vertically we have to give the body a height value, because the password generator or the <div></div> with a class name of “main-container” has already a height of 500px. but 500px of what? the <div></div> needs a reference to position itself to.So it will be 500px of the body. But the body does not have any height for the moment, that’s why we need to set a height for the body.

We’ll add justify-content and give it a value of center.

And align-items property with a value of center.

So the code is:

CSS code for the body

Final step is to go back to the <div></div> with the class name of “main-container” and change the style of the mouse cursor into a pointer and make the caret disappear.

So the CSS code is:

main-container CSS code

The final results looks like this on the browser:

Final results

2) Step 2: Javascript Part

Now that we’re finished with the HTML and CSS part, let’s go to JavaScript.

First, let’s select all the inputs, the button and the <div></div> where the password will show using document.querySelector syntax().

And let’s say that we want to generate a password that is 15 characters long.

So the code is:

Selecting DOM elements

As we want to generate a password made of upperCase letters only, or numbers only, or symbols only, we will declare 3 different variables, one for the upperCase letters where we will store letters, one for numbers and one for symbols.

So the code becomes:

Declaring variables in javascript file

Now we want to generate a password at the click of the button. So when the button is clicked, we want a function to be executed, which is generating the password.

So to make the button listen to the click event the code will be:

Button click event

Now we want at the click of the button, to generate a password. But the length of this password should be only 15 characters.

So to do that we’ll add a for loop that will execute 15 times before generating the password.

so the code will be:

For loop added to the code

Now we will add conditions. We will say, if the upperCase checkbox is checked then I want you to generate a password 15 characters long made of upperCase letters only.

To do that we’ll add the following code:

Code to generate a code with Uppercase letters only

So here we said if the upperCase checkbox is checked, generate a random 15 characters long password, and put it in the password <div></div> container.

To choose random letters we used Math.random() syntax. Math.random() returns a number between 0 and 1. But the number is a floating number. We want an integer, not a floating number. so we introduced Math.floor(), because Math.floor() rounds the number down to the nearest integer.

We used also number 26, because Math.floor(Math.random()*26) will return a number from 0 to 25, which is exactly the number of upperCase array indexes. upperCase array contains 26 letters and their index number starts from 0 and goes up to 25.”black” is here to make the password appear in a black color, because in CSS when we set the color for all the text inside the <div></div> main container, we set the color to white.

So now after checking the box and clicking on the button we have:

UpperCase password generated

But now, if we try to click the button again we’ll have:

Button clicked for the second time

We have now 2 passwords in the same place and shifted to the bottom. Why? Because simply we did not clear the <div></div> that has class name of “password” first before trying to display another password in it.

To display another password in the same <div></div> we have to clear it first, and then we can display something.

To clear the <div></div> we’ll create a function:

Function to clear the div

passwordContainer.replaceChildren() will empty the <div></div> from all nodes, once it is done we want the <div></div> to have the copy/paste emoji on it. Hence passwordContainer.innerHTML() syntax.

So once we have our function we will place it now in the button click event function like this:

Function added to the code

Which means, whenever I click on the button, the <div></div> will be cleared first and then the password will be displayed. If you try now to click on the button as many times as you want you’ll see that you have only one password that is displayed each time you click.

After that we will just repeat the same code with else if statement and change the name of the arrays and checkboxes.

else if statement

So now, whenever we check a checkbox we have a password generated:

Different passwords generated

Now let’s say that we want a mixed password. A password where there are uppercase letters for example and symbols.

To do that we can add another line of code with else if statement saying, if the uppercase and symbols checkboxes are both checked, generate a mixed password:

else if statement to have mixed password

However, if you try now and check both checkboxes you will not have a mixed password, you will still have only a password with the upperCase letters generated. Why? Because the if statement verifies a condition starting from the beginning, and once it meets the condition it executes the code of this condition.

Here we put a condition on both the symbol checkbox and the upperCase checkbox, but this condition is at the end of the code, so when we will check the checkboxes, the if statement will see if there is any condition about the upperCase checkbox, if she finds it, she will execute the code, if not, she will go to the next condition.

So once we check both checkboxes and click on the button the verification will start, and the first condition is about the upperCase checkbox. So when the if statement will find this condition she will stop there, because indeed, the upperCase checkbox ix checked.

To solve this problem, we’ll add the condition about symbol checkbox inside the if statement of the upperCase checkbox.

So the code becomes:

if inside if

This way when the if statement will come across the upperCase checkbox she will verify first if the symbol checkbox is checked or not, if yes then it will generate a mixed password, and if not then it will generate a password with only upperCase letters.

As we want a mixed password, we concatenated both arrays, upperCase and symbols arrays into one array.

So now once both boxes are checked we’ll have:

Mixed password

If we wanted to have a mixed password of symbols and numbers the code would have been:

Mixed password of numbers and symbols

And the results on the browser:

Mixed password (Numbers and symbols)

Now let’s move to the emoji part. It is time to add some code so that when we click on the copy/paste emoji, the password is copied.

To do that first we’ll add an input of type “text” in the HTML file and we’ll give it a class name of “hiddenInput“.

Input added to the HTML

And then we’re going to hide this input using CSS:

Input hidden

Why did we add an input and hid it? Because we will put the password in the <input> and then we can copy it. It is easier to copy elements from an <input> or a <textarea> .

If you want to avoid creating an input at this stage you can set the <input> area where the password will be displayed instead of <div></div> from the beginning.

Now let’s add some JavaScript.

We’ll start by selecting the <input> element and store it in a variable called hidden.

Input selected

Now, we will select the emoji. We’ll go to the clear() function we created, and there as we added the emoji to the <div></div> every time it is cleared, we will tell our function to add a class with a name of “myStyle” every time the <div></div> cleared and the emoji added.

Emoji selected

And we’ll create one last function called selectPassword().

selectPassword() function

In this function we selected the the emoji and put it in a variable called newSpan.

Then we added an eventListener, so that when we click on the emoji, the password is copied.

So at the click we want the <div></div> value to be split, that’s why we have passwordContainer.split(“”) syntax. This syntax will give us an array like this:

splitPassword results

As you can see, the first element of the array is the emoji, and the rest is the password. We want to copy only the password, so we will remove the 1st element of the array that has index 0. Hence the syntax splitPassword.splice(0,1).

Now that we removed the 1st element of the array, we will join the rest of the elements to give us the password displayed on the screen. So we used splitPassword.join(). Now once the array elements are joined, we will put this string in the <input> element we added, and to do that we’ll use hidden.value.

Now the password is inside the <input> element, we will select all the area where the password is using syntax.

And finally we can copy the password by adding navigation.clipboard.writeText(). This syntax is asynchronous, so we have to add a then() method.

Now once the password is copied in the console we’ll have the message: value “Password” successfully copied.

Success message

So we have our function all set, all what is left to do is to call it inside the button.addEvetListener() code.

Function calling

So that’s all for this project. Hope this was useful and you can find the complete code below.

index.html file:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Password Generator</title>
    <link rel="stylesheet" href="index.css">
    <div class="main-container">
        <h1>Password Generator</h1>
            <div class="password">
                <span>	&#10064;</span>
                <p>Upper Case Only
                   <input type="checkbox" class="uppercase"/>
                <p>Lower Case Only
                   <input type="checkbox" class="lowercase"/>
                     <input type="checkbox" class="number"/>
                     <input type="checkbox" class="symbols"/>
        <button>Generate Password</button>
    <input type="text" class="hiddenInput">
    <script src="index.js"></script>

index.css file:

.main-container {
    width: 20%;
    height: 500px;
    background-color: blueviolet;
    border-radius: 20px;
    color: white;
    cursor: pointer;
    caret-color: transparent;

.password {
    width: 80%;
    height: 50px;
    background-color: white;
    margin: 0 auto;
    border: 5px outset blue;

span {
    position: relative;
    left: 87%;
    top: 30%;
    font-size: 30px;
    color: black;

p {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 7px;
    font-size: large;

hr {
    width: 90%;

button {
    display: block;
    margin: 10% auto;
    padding: 10px;
    border-radius: 15px;
    color: rgb(17, 17, 98);
    font-size: large;
    background-color: white;

button:hover {
    background-color: rgb(28, 209, 222);
    color: white

body {
    display: flex;
    height: 90vh;
    justify-content: center;
    align-items: center;


.hiddenInput {
    visibility: hidden;

index.js file:

var button = document.querySelector("button");
var passwordContainer = document.querySelector(".password");
var upperCaseInput = document.querySelector(".uppercase");
var lowerCaseInput = document.querySelector(".lowercase");
var numberInput = document.querySelector(".number");
var symbolInput = document.querySelector(".symbols");
var passwordLength = 15;
var hidden = document.querySelector(".hiddenInput");

var upperCase = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
    "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
    "W", "X", "Y", "Z"];

var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var symbols = ["@", "%", "*", "/", "~", "&"];

button.addEventListener("click", () => {
    for (var i = 1; i <= passwordLength; i++) {
        if (upperCaseInput.checked == true) {
            if (symbolInput.checked == true) {
                var concatenatedArray = upperCase.concat(symbols);
                passwordContainer.innerHTML += concatenatedArray[Math.floor(Math.random() * 31)];
       = "black";

            } else {
                passwordContainer.innerHTML += upperCase[Math.floor(Math.random() * 26)];
       = "black";

        } else if (lowerCaseInput.checked == true) {

            passwordContainer.innerHTML += upperCase[Math.floor(Math.random() * 26)].toLowerCase();
   = "black";

        else if (numberInput.checked == true) {
            if (symbolInput.checked == true) {
                var concatenate = numbers.concat(symbols);
                passwordContainer.innerHTML += concatenate[Math.floor(Math.random() * 15)];
       = "black";
            } else {
                passwordContainer.innerHTML += numbers[Math.floor(Math.random() * 10)];
       = "black";
        else if (symbolInput.checked == true) {
            passwordContainer.innerHTML += symbols[Math.floor(Math.random() * 6)];
   = "black";


function selectPassword() {
    var newSpan = document.querySelector(".myStyle");
    newSpan.addEventListener("click", () => {
        var splitPassword = passwordContainer.textContent.split("");
        splitPassword.splice(0, 1);
        hidden.value = splitPassword.join("");;
        navigator.clipboard.writeText(hidden.value).then(() => console.log("value " + hidden.value + " successfully copied"));

function clear() {
    passwordContainer.innerHTML = "<span>&#10064;</span>";
    var mySpan = document.querySelector("span");


Leave a Reply

%d bloggers like this: