Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:13:10+00:00 2026-06-15T06:13:10+00:00

i have the following code, that randomizes text from an array. However what im

  • 0

i have the following code, that randomizes text from an array.

However what im not sure how to do the following.

  1. display the result on browser load instead of onclick.
  2. display the output to a div CLASS on load (so if i have multiple divs called “output”, a random font/size/weight will be displayed in each div)
  3. it should take the font from each div element and change the classes etc, instead of using the font names etc.
  4. instead of using the font names in an array, how do i point each variable to a class?

    .font1 {font-family:Verdana, Geneva, sans-serif;}
    .font2 {font-family:Georgia, “Times New Roman”, Times, serif;}
    .font3 {font-family:”Trebuchet MS”, Arial, Helvetica, sans-serif;}
    .font4 {font-family:”Times New Roman”, Times, serif;}
    .font5 {font-family:”Comic Sans MS”, cursive;}

    var fonts = [‘.font1’, ‘.font2’, ‘.font3’, ‘.font4’, ‘.font5’];

really hope someone can help.

code is as follows;

<button onclick="run()">Test</button>
<div id="output"/>

var fonts = ['Arial', 'Helvetica', 'Georgia', 'Tahoma', 'Verdana'];
var weights = ['normal', 'bold', 'lighter'];
var sizes = ['16px', '20px', '24px', '36px', '40px'];

var choose = function(arr) {
return arr[Math.floor(Math.random() * arr.length)];
};
var output = document.getElementById("output");

window.run = function() {
var text = choose(sizes) + ' ' + choose(weights) + ' '  +  choose(fonts);
output.innerHTML = '<div class="randomFont" style="font: ' + text + '">' + text +      '</div>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:13:11+00:00Added an answer on June 15, 2026 at 6:13 am
    1. You can attach an event listener to the window load event:

      window.addEventListener('load', run, false);
      

      Better yet, if the browsers you’re targeting support it, you could use DOMContentLoaded, which will not wait for images, stylesheets, and other less-essential resources to load:

      document.addEventListener('DOMContentLoaded', run, false);
      

      You could even use a combination of the two: attach to both, and in run, only do it when it has not already been done.

    2. You can find all of the elements with a given class using document.getElementsByClassName. You can then iterate over the returned collection and give a random font to each of those.

    3. (and 4.) Rather than constructing some HTML and a CSS string, you could instead use the style property for the weights and sizes. For example:

      element.style.fontWeight = 'bold';
      

      For the font family, select one of your classes and append a space and then it to the element’s className property:

      element.className += ' ' + fontFamilyClass;
      

    Try it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When
I have the following code that loads a plist file from the resource folder
I have the following code that allows me to load a set URL each
I have the following code that extracts urls from a given page using jsoup.
I have the following code that displays a JDialog , it shows a text
I have the following code that makes a sub form from a table, I
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code snippet that i use to compile class at the run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.