I am currently developing an application that can produce random video game suggestions and display the corresponding game cover upon generation

I'm facing an issue with displaying the video game cover image from my HTML document when the game title is generated using a JavaScript function. I have searched online for solutions, but couldn't find anything similar to my problem.

Here is my HTML:

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Game Generator</title>
  </head>
  <body>
    <header id="header">
      <div id="title">Video Game Generator</div>
    </header>
    <section>
      <div id="container">
        <img src="images/helldivers2-cover.avif" id="game0" />
        <img src="images/skyrim-cover.jpg" id="game1" />
        <img src="images/cyberpunk2077-cover.png" id="game2" />
        <img src="images/witcher3-cover.jpg" id="game3" />
        <img src="images/gtav-cover.png" id="game4" />
        <img src="images/starfield-cover.webp" id="game5" />
        <img src="images/dbd-cover.avif" id="game6" />
        <img src="images/arkse-cover.jpg" id="game7" />
        <img src="images/acv-cover.jpg" id="game8" />
        <img src="images/rdr2-cover.jpg" id="game9" />
        <ul id="game"></ul>
      </div>
      <div id="btn">
        <button onclick="generateGames()">Generate</button>
      </div>
    </section>

    <script src="script.js"></script>
  </body>
</html>

Here is my CSS:

    @import url('https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Poppins', sans-serif;
  background-color: rgb(37, 37, 37);
}

#header {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 150px;
}

#title {
  color: white;
  font-size: 50px;
}

#container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 550px;
}

#game0 {
  display: none;
  padding-top: 50px;
  height: 500px;
  width: 300px;
}

#game1 {
  display: none;
  padding-top: 50px;
  height: 500px;
  width: 300px;
}

#game2 {
  display...

Here is my JavaScript:

// Functionality and code are described here...

The challenge lies in getting the game cover to display along with the respective game title. If you have any suggestions or insights on how to resolve this issue, please let me know!

Answer №1

Don't forget to utilize your cover() function to make things work as expected. However, simply calling it won't be enough; you'll also need to take care of the following:

  • Specify which cover you want to display
  • Ensure that other covers are hidden before showing the desired one
  • Adjust the display style to ensure visibility

Here's a sample function for reference:

function cover(selectedGame) {
  document.querySelectorAll('img').forEach(img => img.style.display = '');
  document.getElementById(`game${selectedGame}`).style.display = 'block';
}

This function first resets the display style for all <img> elements, allowing them to inherit their default styles from CSS (which is currently set to none).

It then targets the specific <img> by its unique id, setting its display property to block for visibility.

Make sure to include a call to this function within generateGames():

const selectedGame = games[randomGame];
gameElement.innerHTML = selectedGame;
usedGames.add(randomGame);
cover(randomGame); // <--- Call to function here
break;

Answer №2

Developing an application that randomly generates a video game and showcases its cover is a brilliant concept for gaming aficionados! This app has the potential to be a valuable tool for users who struggle with deciding what game to play next. By incorporating filters such as genres, platforms, and release years, it can cater to a variety of gaming preferences. Including features like game descriptions, reviews, and links to purchase could enhance its interactivity and usefulness. With a sleek design and a dash of ingenuity, this app could easily become a favorite resource for discovering hidden gaming treasures.

As you brainstorm gaming concepts, don't overlook the captivating simplicity of Money Rush, a game designed for quick and enjoyable breaks. In Money Rush, players strategize to increase their wealth by maneuvering through obstacles and making strategic decisions, all while being captivated by its fast-paced and addictive gameplay. Similarly, your app could provide a swift and thrilling avenue for users to explore new games, mirroring the instant gratification offered by Money Rush. Whether users are uncovering random games via your app or delving into the excitement of Money Rush's mechanics, both experiences promise unique and pleasurable moments in the realm of gaming!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to initiate a download via a GET request in axios

I am currently utilizing Vue.js 2 with Axios to send a GET request and pass parameters to the server in order to retrieve a PDF as a response. The server is powered by Laravel. Having issues with force downloading the PDF even though the correct headers a ...

Include the referrer header when using the chrome.downloads API

I have been working on developing an extension that replicates the Ctrl+Click to save image functionality from Opera 12. In my extension, I am utilizing chrome.downloads.download() in the background script to trigger downloads, while a content script is re ...

Is there a way for me to transfer a variable to a URL?

I'm attempting to send an email that includes a link with a value obtained from Firebase. While I can successfully retrieve the value, I am unsure how to add it to the existing link. Here is my code snippet: sendinvite() { var user = firebase.a ...

Hiding the button until either hovering over the div or entering text into the input field

I'm looking to create the following structure: <div class-name="search"> <input class-name="input"/> <button class-name="searchbutton> search </button> </div> However, I would like the button to initially be hidden. ...

Steps for creating an HTML report using Intern JS

Our team relies on intern JS for automating functional tests, however we are facing difficulty in generating an html report. I attempted to use locvhtml as suggested by the Intern documentation (https://theintern.github.io/intern/#reporter-lcov), but unfo ...

Is there a way to apply multiple filters to the Redux state in a React project?

This is the state in my Redux store: products: [ { id: 1, name: 'Ps4', price: 4500000, number: 0, inCart: false, category: 'electronics', b ...

Display the spinner until the data is successfully updated in the DOM using Angular

Dealing with a large dataset displayed in a table (5000 rows), I am attempting to filter the data using column filters. When applying the filter, I have included a spinner to indicate that the filtering operation is in progress. However, due to the quick ...

The index.html file refuses to load despite the node server running smoothly

As a newcomer to AngularJS, I recently experimented with a new code snippet using $http. After setting up nodeJS and installing express via npm, I ran server.js on node - node server.js, which displayed the "listening at port" message. However, when attemp ...

How can I transform my JavaScript code into ReactJS?

Is it feasible to transform certain JavaScript code into a ReactJS component? Could anyone lend me a hand with this, please? ...

The webpage lacked the functionality of smooth scrolling

I created a website which you can view here. Check out the code below: <div id="mainDiv" class="container"> <div id="header"> <div class="plc"> <h1><a href="#"></a></h1> ...

Function executed many times during click action

I have developed a web application that allows users to input any keyword or statement and receive twenty results from Wikipedia using the Wikipedia API. The AJAX functionality is working perfectly. The app should dynamically create a DIV to display each r ...

Ways to split images and text in list items using CSS

Can the text be formatted in a specific location on the page, separate from the image and heading? This is the HTML code I am currently using: <div class="tab-pane container fade" id="environmental"> <div class="row"> <div class="c ...

Ways to adjust the placement of an input field on a webpage

I'm attempting to align the drop-down boxes and the input box on the same line, but for some reason the input box keeps floating to the bottom. Check out the image showing the floating boxes <div style="margin-left: 2%; font-size:10";> < ...

Adding Rows to Excel Using Powershell and Arrays

I have been able to update Excel spreadsheet cells one by one using a COM object, but it's very slow. I have an array of about 600 objects representing user details, each with 15 properties (either strings or booleans). How can I update an entire row ...

What is the correct way to utilize preloads and window.api calls in Electron?

I am struggling with implementing a render.js file to handle "api" calls from the JavaScript of the rendered HTML page. The new BrowserWindow function in main.js includes: webPreferences: { nodeIntegration: false, // default value after Electr ...

Conceal location labels in maps provided by tilehosting.com

I am currently in the initial stages of developing a maps web application and I want to incorporate maps from tilehosting.com along with the leaflet library (leafletjs.com). Overall, it seems to be working fine, but I'm struggling with hiding the def ...

What is the best way to determine if a text matches any of the elements in

Seeking assistance with a demo I am working on. How can I perform a precise comparison between test and the elements in an array arr1? var arr1 = ['noël','noel']; var test = 'noel; if(){ } <script src="https://ajax.google ...

I am having trouble updating a record in my database table

I am encountering an issue while trying to update a row in a table using a form. The add button works fine, but I am unable to update it using the update (a tag). Just a reminder: the edit button is located in the last column of each row along with the de ...

Is HTML-React-Parser giving back an object instead of a string?

Currently, I am attempting to convert basic HTML code into JSX format using HTML-React-Parser. To achieve this, I have included the script in my HTML document as shown below: <script src="https://unpkg.com/html-react-parser@latest/dist/html-react- ...

Is it better to use scale.set() or simply increase the size of the model in Three.js?

When it comes to scaling 3D models in Three.js (or any other 3D renderers), what is considered the best practice? Recently, I encountered a situation where I loaded a model only to realize that its size was too small. In order to adjust the size, I used m ...