CxD Archive
Code: Trivia App
Code: Trivia App
  • About
  • Prerequisite Knowledge
  • Code Template
    • Template Build Tutorial
      • 1. Screens as Containers
      • 2. Welcome Screen
      • 3. Question Screen
      • 4. Thank You Screen
      • 5. Core JS Functions
      • 6. Load Questions and Go
      • 7. Respond to Answer Clicks
      • 8. Finishing the Game
      • 9. Visual Enhancement
    • Managing the Question DB
  • More
    • Code Mod Examples
      • Style Changes with CSS
      • Support True/False Questions
      • Add "Next Question" Button
      • Add a Category Selection Screen
      • Add Sound Effects on Answer Clicks
      • Add Basic Scoring
      • Add a Question Timer
      • Add a Question Timer with Pause Button
      • Custom Feedback Text for Individual Questions
      • Use Images with Questions
      • Display the Total Question Count at Welcome
      • Add Background Animation
    • Trivia JS Library Reference
    • P5.js
    • ★ Trivia Project Guidebook
Powered by GitBook
On this page
Export as PDF
  1. More
  2. Code Mod Examples

Use Images with Questions

PreviousCustom Feedback Text for Individual QuestionsNextDisplay the Total Question Count at Welcome

Last updated 6 years ago

If you want questions to display a particular image, like shown above, here are the basic steps.

1 - Upload Images to Your App in an "images" folder.

  • index.html

  • style.css

  • code.js

  • images

    • question1image.png

    • question2image.png

2 - Add a Column in Your Spreadsheet for Image Names

3 - Add HTML to Hold the Image

You may want to place this right below your question, like the example at the top of this page.

<div id="image-holder"></div>

4 - Add the Javascript to Display the Image

You will want to add this right after trivia.insertQuestionInfo(); in your displayQuestion( ) function.

if (trivia.currentQuestion.image)
    $('#image-holder').html(`<img src='images/${trivia.currentQuestion.image}'</img>`);
else 
    $('#image-holder').html(``);

5 - Add CSS Styles

These styles will maintain the width of the image and center it on the screen. Change the styles as needed.

#image-holder {
  margin: auto;
  width: 150px;
}

#image-holder > img {
  width: 100%;
}

That's it. It should work for you now.