Struggling with displaying and hiding div elements

Here is the code I'm currently working on: https://jsfiddle.net/Metalnoypi/d7ocf929/#&togetherjs=HD16R9BLLF

I am relatively new to JavaScript, HTML, and CSS. I am facing an issue where the main menu page should disappear when the start button is clicked, revealing the word "Test". I could use some recommendations on how to achieve this functionality along with a brief explanation of how it works.

<menu>
<div id="menu">
  <body background="">
<divcenter>
<font color="blue"><font size="5"><center><h2> Realm of Rilthalk </h2></center></font></font>
<font color="magenta"><font size="5"><center><h3>The Origin Story</h3></center></font></font>
</divcenter>
<divbottomleft> <input type="button" id="start" class="hide" value = "Start Game"/> </divbottomleft>
<divbottomright><input type="button" id="quit" value = "Quit"/>
</divbottomright>
</body>
</div>
</menu>
<game>
<div id="game" class="toshow" style="display:none" >
<divcenter><font color="jade"><H1><center> Test </center></H1></font>
</divcenter>
</div>
</game>
<script>
$(document).ready(function(){
$(".hide").click(function(){
    $(this).parents("menu").hide();
        $("game").parents("game").toggle();    
});
});

</script>

This is my CSS Code:

divcenter {
height: 200px;
width: 400px;
position: fixed;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -200px;
}

divbottomleft {
position: fixed;
top: 50%;
left: 50%;
margin-top: 75px;
margin-left: -200px;
}

divbottomright{
position: fixed;
top: 50%;
left: 50%;
margin-top: 75px;
margin-left: 130px;
}

Answer №1

Your understanding is on the right track, but there are minor adjustments needed in your jQuery code. It's actually quite straightforward; you just need to hide the menu and reveal the game within the same click function:

$(document).ready(function() {
  $(".hide").click(function() {
    $("#menu").hide();
    $("#game").show();
  });
});
divcenter {
  height: 200px;
  width: 400px;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -200px;
}

divbottomleft {
  position: fixed;
  top: 50%;
  left: 50%;
  margin-top: 75px;
  margin-left: -200px;
}

divbottomright {
  position: fixed;
  top: 50%;
  left: 50%;
  margin-top: 75px;
  margin-left: 130px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<menu>
  <div id="menu">

    <body background="">
      <divcenter>
        <font color="blue"><font size="5"><center><h2> Kingdom of Aleria </h2></center></font></font>
        <font color="magenta"><font size="5"><center><h3>The Epic Saga</h3></center></font></font>
      </divcenter>
      <divbottomleft>
        <input type="button" id="start" class="hide" value="Begin Adventure" /> </divbottomleft>
      <divbottomright>
        <input type="button" id="quit" value="Exit Game" />
      </divbottomright>
    </body>
  </div>
</menu>
<game>
  <div id="game" class="toshow" style="display:none">
    <divcenter><font color="jade"><H1><center> Challenge Accepted </center></H1></font>
    </divcenter>
  </div>
</game>

I've created a JSFiddle to demonstrate this concept here.

Hopefully, this explanation clarifies things! :)

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

Strip away all HTML attributes within a string

I am in the process of developing an internal tool that allows a designer to input exported svg code into a text area and have the html code displayed in a syntax highlighter () When they paste their code like this <svg xmlns="http://www.w3.org/20 ...

Django was unable to load the static image

Currently, my Django application is hosted on PythonAnywhere. In order to generate a PDF, I am utilizing WeasyPrint which includes an image that should be displayed in the PDF. However, within the error log, I am encountering the message "Failed to load im ...

What is the purpose of pressing the button a second time once the Conditional Statements are no longer

$(function() { $("button").click(function() { if ($(this).text() === "3") { console.log("correct"); $(this).text("correct") } else { console.log("wrong"); $(this).text("wrong") } }) }) <script type="text/javascri ...

Adjustable image scaling with dynamic maximum height

Instead of trying to explain the problem I need to solve, let me show you an example for better clarity (apologies to mobile users, this may not work...) You can observe the effect I am aiming for on VICE news () While resizing the browser, notice how th ...

Tips for optimizing AngularJS performance with large scopes

Currently, I am working on an Angular app for my company and have encountered a major issue. The app has become extremely slow, even after trying to optimize it using techniques like onetimebind and track by. Despite initial improvements, the performance i ...

I am attempting to set up React, but I keep encountering an issue that says 'Error: EPERM: operation not allowed'

My attempts to set up React have been unsuccessful so far. Initially, I tried installing it using the command npx create-react-app ./, which did not work. Then, I attempted to run npm init react-app my-app, but unfortunately, that also failed. The error me ...

What is the purpose of enclosing an Angular app within a function?

As I delve into learning Angular, I've encountered a recurring snippet in the app.js file across various resources: (function () { \\\myAngularModules })(); Despite its prevalence, the explanation provided is often just ...

Tips for concealing the background HDR map display in three.js without compromising the HDRI's contribution to the lighting effects

In my code, I have implemented the following example: Source: https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_gltf.html#L53 While HDRI lighting works perfectly, I am struggling to hide the HDR map from the background. Is there a way ...

What is the best way to make ng-style append to a pre-existing style?

I am facing an issue with my custom popover directive and another custom directive that uses it. I am attempting to use ng-style to adjust the width of the popover. Below is a code snippet from the directive's html template: <div my-custom-popover ...

Problem with Ajax functionality on Jquery Dialog

I have implemented a Jquery.dialog feature in my application for sending messages. When the user clicks on "new", the dialog box appears, allowing them to select the recipient (currently working with IDs instead of usernames). On the first opening of the ...

Having trouble installing @angular/cli 4 on Debian?

I'm having trouble installing @angular/cli on my Debian box. I already have the latest versions of Node.js and npm installed. Interestingly, Angular4 works perfectly fine on my Windows machine where I use it daily. However, when I try to get it runnin ...

Adjust the appearance of a div according to the input value

When a user inputs the correct value (a number) into an input of type "number," I want a button to appear. I attempted var check=document.getElementById("buttonID").value == "1" followed by an if statement, but it seems I made a mistake somewhere. Here&ap ...

Embedding Views in a <td> tag with DataTables and Ember.js

Has anyone ever attempted to incorporate a view within a using data tables? Currently, I am loading the data and dataTables through didInsertElement. Within each record, I am adding an "edit" and "delete" button inside a td so users can click to edit or d ...

Animating a straight line between two points on a webpage using HTML5 and JavaScript

Here's a simple question that might have a solution... I am interested in creating an animation that moves between two points in a straight line, using HTML5 and/or JQuery. ctx.beginPath(); ctx.moveTo(0, 0); // starting point ctx.lineTo(100, 100); / ...

I've noticed that my alert is not appearing when I click submit. Can someone please help me figure out what I could

I am having trouble getting my alert to display when the form is not validating. It should show an alert message if the form is valid but it's not working as expected. I've spent hours trying to fix this issue with no luck. I appreciate any help ...

Ways to acquire the reference of the parent component

Within my code, there is a parent component represented as: <Parent> <Child (click)=doSomething()></Child> </Parent> I am looking to establish a reference to the Parent component in order to pass it within a method that will trigg ...

Tips for maintaining a single-line div while adding ellipses:

What is the CSS way to ensure that a div or class contains only one line of text, with ellipses added if it exceeds that limit? I am aware that setting a specific height and using overflow:hidden can achieve this, but it doesn't quite work for my need ...

Create a boundary behind the title on a see-through backdrop

How can I create a design where there is a line to the left and right of a headline? Usually, I would use border-top on the surrounding element and style the headline with some CSS properties like this: h2 { margin-top: -10px; padding: 0 5px; b ...

Unable to upload a file to an email using the mandrillapp JSON API

Having trouble sending an email with an attached document via the Mandrillapp JSON API using the method send-template in JavaScript. The email sends successfully, including images attached to the images array. However, documents sent in the attachements ar ...

The collapsible menu is not expanding properly after a click event due to the small size of

My website has a menu placed in the header with a background color. When resizing the page (max width 1044 px), it switches to a mobile menu, but the div holding the menu and anchors is too small compared to the header. Is there a solution to this issue? ...