What could be the reason my div is not displaying correctly?

I am currently developing a game using HTML, CSS, and jQuery. The game will involve falling blocks that the player needs to avoid.

However, I am facing an issue with my jQuery implementation. When selecting a theme (only the dark theme is currently functional), the player does not appear on the screen:

$(document).ready(function() {

  $('#options').change(function() { // NOTE: all themes have capital letters on colors
    if ($(this).val() === 'Dark') {
      $("#game").css("background-color", "black");
      $("#player").css({
        "background-color": "white" // utilizing CSS function for potential additional styling
      });
    }
  });

  $("#play").click(function() {
    $(this).hide();
    $("#options").hide();
    $("#player").show();
  });


});
body {
  background-color: #FFFFFF;
  font-family: helvetica, sans-serif;
}
.block {
  width: 60px;
  height: 60px;
  position: absolute
}
#game {
  width: 1000px;
  height: 550px;
  border: 3px solid #000000;
  margin: 2% 10%;
}
#player {
  width: 40px;
  height: 40px;
  left: 50%;
  bottom: 0;
  position: absolute
}
#play {
  padding: 1%;
  color: white;
  background-color: black;
  border-style: solid;
}
#options {}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Trash Fall - Game</title>

  <link rel="stylesheet" type="text/css" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <!-- adding jQuery to the script -->
  <script src="script.js"></script>
</head>

<body>

  <h1 id="main_title">Color Drop</h1>

  <div id="game">

    <button id="play">Play</button>
    <select id="options">
      <option value="none">none (choose one in order to play)</option>
      <option value="Dark">Dark</option>
      <option value="Light">Light</option>
      <option value="Blue_White">Blue/White</option>
      <option value="Red_White">Red/White</option>
    </select>

    <div id="player"></div>

  </div>

</body>

</html>




NOTE: While this code snippet appears to work, it is not functioning as intended on GitHub (where I am building the game): example GitHub link

Could you please advise why this is causing issues?

Additionally, why are the theme and player elements not displaying correctly within the game section upon theme selection?

Github repository link: https://github.com/FlipFloop/Color-Drop

Thank you!

Answer №1

#player has a CSS property of position: absolute;, but #game is not positioned, causing the player to appear at the bottom of the document, typically invisible due to being white on a white background. To fix this issue, add position: relative; to the #game element.

Additionally, consider the following code snippet provided by @FlipFloop as an answer:

$(document).ready(function() {

  $('#options').change(function() {
    if ($(this).val() === 'Dark') {
      $("#game").css("background-color", "black");
      $("#player").css({
        "background-color": "white"
      });
    }
  });

  $("#play").click(function() {
    $(this).hide();
    $("#options").hide();
    $("#player").show();
  });


});
body {
  background-color: #FFFFFF;
  font-family: helvetica, sans-serif;
}
.block {
  width: 60px;
  height: 60px;
  position: absolute
}
#game {
  width: 1000px;
  height: 550px;
  border: 3px solid #000000;
  margin: 2% 10%;
  position: relative; /* Add this line */
}
#player {
  width: 40px;
  height: 40px;
  left: 50%;
  bottom: 0;
  position: absolute;
}
#play {
  padding: 1%;
  color: white;
  background-color: black;
  border-style: solid;
}
#options {}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Trash Fall - Game</title>

  <link rel="stylesheet" type="text/css" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <!-- adding jQuery to the script -->
  <script src="script.js"></script>
</head>

<body>

  <h1 id="main_title">Color Drop</h1>

  <div id="game">

    <button id="play">Play</button>
    <select id="options">
      <option value="none">none (choose one in order to play)</option>
      <option value="Dark">Dark</option>
      <option value="Light">Light</option>
      <option value="Blue_White">Blue/White</option>
      <option value="Red_White">Red/White</option>
    </select>

    <div id="player"></div>

  </div>

</body>

</html>

Answer №2

To effectively handle different options data, it is recommended to utilize conditional statements such as switch or if/else if/else in your code. Currently, the code only caters to the Dark value.

My experience with implementing a Light condition proved successful:

$(document).ready(function() {
        console.log("working fine");

  $('#options').change(function(){ // Please note: all themes are specified with capital letters for colors
      console.log("working fine");
    if($(this).val() === 'Dark'){ 
      $("#game").css("background-color", "black");
      $("#player").css({
        "background-color": "white" // Using CSS function allows for additional customization
      });
    }
    if($(this).val() === 'Light'){ 
      $("#game").css("background-color", "white");
      $("#player").css({
        "background-color": "white" // Using CSS function allows for additional customization
      });
    }
  });

  $("#play").click(function() {
    $(this).hide();
    $("#options").hide();
    $("#player").show();
  });


});

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

What is the best way to isolate the text before a particular segment within a string, excluding any blank spaces?

Is it possible to use vanilla JavaScript in order to extract the first string value directly to the left of a specified portion of a longer string, excluding any blank spaces? Consider the following string: 1 hr 30 min How could you extract the 1 located ...

The download button consistently targets and downloads the initial SVG in my collection. How can I configure it to target each individual SVG for download?

I'm currently working on a QR code app using React. The goal is for users to submit a form with a value, which will then generate an SVG QR code that can be downloaded. However, I'm running into an issue where the same SVG file is being downloade ...

What's the reason for this Ajax request taking such a long time to display on the

My webpage features dynamic content created by Ajax, refreshing every 30 seconds with new information from the database. The PHP side of things seems to be functioning properly, but I suspect there might be an issue either in my JavaScript code or within t ...

Experiencing trouble with retrieving the selected image source and showing it in a text field

Looking for help with a javascript issue related to image selection. My goal is to store the selected image's src in a variable and display just the folder ID and image ID without the path and file extension. I want this process to happen in real time ...

Find out the index of a div within an array

Curious about determining the position of a div in an argument? For instance, if there are multiple divs with the same name: <div class="woot"></div> <div class="woot"></div> <div class="woot"></div> <div class="woot ...

Uncovering data treasures on the web with BeautifulSoup: Extracting JSON files from DIV elements made simple

As I attempted to extract data from a website using standard class names for the required elements, I came across an interesting discovery. Near the top of the HTML code, there was what appeared to be a JSON file containing all the necessary information. ...

Unlocking the AngularJS variable within the template script

My controller code: $scope.totals = totals; In the template, I am able to successfully inject HTML using this code: {{totals}} However, my goal is to access the totals variable in a script within the template. Here's what I attempted: <script& ...

Choose an option with JavaScript once the request is successful

Choose the day, month, and year using JSON data success: function(jsondata){ var data=JSON.parse(jsondata); var list_html="<div id='editrelation'><label id='dateLabel' style='display:none&apo ...

Converting a CSS file into a string by importing it into a JavaScript file

Is it possible to manipulate a style tag loaded from an external CSS file using plain JavaScript? I'm attempting to apply custom styles to a style tag fetched from a CSS file, The issue lies in my struggle to convert the file content into a string ...

Ensure that the ng-view element has a height of 100% and

Currently, I am working on developing an AngularJS app and my index page consists of a ng-view where all the content is injected using ui-router: <header> <nav class="navbar"> //content for navbar will go here </nav </hea ...

The element type 'HTMLElement' does not contain a property named 'pseudoStyle'

Currently experimenting with adjusting the height of a pseudo element using Typescript. An error is popping up in my IDE (vscode) as I go along. This is the code snippet I am working with. // choose element let el: HTMLElement = document.getElementById( ...

Creating Beautiful Tabs with React Material-UI's Styling Features

I've been delving into React for a few hours now, but I'm struggling to achieve the desired outcome. My goal is to make the underline color of the Tabs white: https://i.stack.imgur.com/7m5nq.jpg And also eliminate the onClick ripple effect: ht ...

Employing PHP conditions alongside JavaScript to conceal the div elements

I have been struggling with a problem for the past few hours and still haven't found a solution. I am new to php. What I am trying to achieve is to display and hide a div based on a PHP condition. Here is the scenario: Please note: By default, the DI ...

Issues with password confirmation function in Vuetify components

I am struggling to create a registration form where users must confirm their password by typing it in twice, but I can't seem to get Vuetify to validate that the two passwords match. Despite trying to set up rules for this validation, my code doesn&a ...

"The issue seems to stem from a snippet of compact JavaScript

I am facing an issue with a list item that contains both an image and text. The text is overlapping the image, but when I hover over the text, it disappears and only the picture is visible. Here is the HTML code snippet: <ul> <li><img ...

Guide on displaying the name attribute of a button along with its price in a separate div when clicked

Upon clicking the Koala button, I want to display a message showing the id name of the button and the corresponding price for Koalas. Here is the HTML code snippet: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link h ...

The code begins executing even before the function has finished its execution

I believe that is what's happening, but I'm not entirely sure. There's code in a .js file that invokes a function from another .js file. This function performs an ajax call to a php page which generates and returns a list of radio buttons wi ...

How do you efficiently include several elements within a single column in Boostrap 5?

I've been working with Bootstrap 5 to display a grid of images similar to this link, but the images are not displaying as intended due to some CSS code. #projects img { width: 100%; height: 100%; } <section id="projects"> ...

"Struggling with setting the default checked state for single and multiple checkboxes? The ng-init method with checkboxModel.value=true seems to be ineffective – any suggestions

<input type="checkbox" ng-model="isChecked.value" ng-true-value="'yes'" ng-false-value="'no'" ng-click='handleCheckboxChange(isChecked.value, idx);' ng-init="isChecked.value=true" /> ...

What is the best way to use the map function to print an array of arrays containing objects in a React component?

I am dealing with a functional component that takes an array of arrays with objects, but I am struggling with accessing the data in the render return function. let dataItems = [ [ { data1: "1234", data2: "ABCD", ...