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

545
Views
How can I display an random quote from an Javascript Array in html?

im trying to make random quotes that are listed in an array appear on my website on reload however I cant quite get it to work

Javascript:


function textOfToday() {
    
    var textOfTodayArray = [
        'String 1', 'String 2'
    ]

document.getElementById('randomTitle').value = textOfTodayArray[Math.floor(Math.random()*textOfTodayArray.length)];
}

The following html div code is designed to display the text however I just cant get it:

<div class="randomTitle" style="text-align: center;"></div>

Ive also tried using id="randomTitle" in the div part but this did not work either

Is there something else wrong maybe? Im not no expert by all means but I cant spot any syntax mistakes

Any help would be appreciated

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

0

Make sure to call the function in your js textOfToday(). You are defining it, but never calling.

Also, yes, you need to change the class to id if you are going to use getElementById.

Lastly, you need to set innerText instead of value

about 4 years ago · Juan Pablo Isaza Report

0

document.querySelector('.foo').value mostly using for <input>, to display information in the div or whatever element is it, you better use innerText or innerHTML

function textOfToday() {

var textOfTodayArray = [
    'String 1', 'String 2'
]

document.getElementById('randomTitle').innerText = textOfTodayArray[Math.floor(Math.random()*textOfTodayArray.length)];
}

textOfToday()
<div id="randomTitle" style="text-align: center;"></div>

about 4 years ago · Juan Pablo Isaza Report

0

You need to use the innerHTML property to display in the HTML element. Also make use of the id tag when you are targeting by Id. Also, You never invoked your function so it never ran.

function textOfToday() {

  var textOfTodayArray = ['String 1', 'String 2'];

  document.getElementById('randomTitle').innerHTML = textOfTodayArray[Math.floor(Math.random() * textOfTodayArray.length)];
}

textOfToday();
<div id="randomTitle" style="text-align: center;">Hi</div>

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!