Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
How to have all symbols in a string without breaking the html?

I'm playing around with the password generator, but when the string contains a < or > it breaks the HTML and only outputs some of the characters. I'm using innerHTML instead on textContent because I need to wrap each password in a div. Is there another way I can do this without it breaking?

const alphabetUppercase = "abcdefghijklmnopqrstuvwxyz".toUpperCase().split('');
const alphabetLower = "abcdefghijklmnopqrstuvwxyz".split('');
const symbols = "!@#$%^&*(>)_+=-`~,./;'[]\|}\"<{:\?".split('');
const numbers = "123456789".split('');
const masterArray = alphabetUppercase.concat(alphabetLower, symbols, numbers);

const passwordButton = document.querySelector('.generate-passwords');
const outputPasswords = document.querySelector('.output-passwords');

passwordButton.addEventListener('click', function(e){
    e.preventDefault();
    let passwordArray = []; 
    // number of passwords
    let counter = 4;

    // Reset text content
    outputPasswords.innerHTML = '';  

    for (let x = 0; x < counter; x++) {
        let randomPassword = "";

        for (let i = 0; i < 15; i++) {
            let randomOutput = Math.floor(Math.random() * masterArray.length);
            randomPassword += masterArray[randomOutput];
        }
        passwordArray.push(randomPassword);   
        outputPasswords.innerHTML += '<div>' + passwordArray[x] + '</div>';
    } 
});
    <section class="generator">
        <a href="#" class="generate-passwords">Generate passwords</a>

        <div class="output-passwords"></div>
    </section>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do something like this:-

const div = document.createElement('div');
div.append(randomPassword);
outputPasswords.append(div);

Instead of using div as a string, create an element "div" and then append it to the parent "outputPasswords" element

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!