I'm attempting to switch between different "screens" by utilizing divs with CSS and JavaScript, using the display property to toggle between none and block. Everything seems to be functioning smoothly except for the

I thought I grasped the concept of this because all my toggles are working fine, except for the last one. The frustrating part is that it's written in the same manner as the others before it. The problematic button is identified by id="startYearOne". It should toggle to the next div known as "scrControls", but it simply refuses to work unlike the others.

document.getElementById("btnProfessions").addEventListener("click", funcProfessions);
document.getElementById("btnYearOne").addEventListener("click", startYearOne);

function funcProfessions() {

  document.getElementsByClassName("scrProfessions")[0].style.display = "none";
  document.getElementsByClassName("scrChosenCareer")[0].style.display = "block";

  var listProfessions = ["nurse", "worker", "teacher"];

  var randomCareer = listProfessions[Math.floor(Math.random()*listProfessions.length)];
  document.body.innerHTML = document.body.innerHTML.replace('profession', randomCareer);

  switch (randomCareer) {
    case 'nurse':
      document.getElementById("imgCareer").src="images/nurse.png";
      document.body.innerHTML = document.body.innerHTML.replace('0', '1300');
      break;
    case 'worker':
      document.getElementById("imgCareer").src="images/worker.webp";
      document.body.innerHTML = document.body.innerHTML.replace('0', '1000');
      break;
    case 'teacher':
      document.getElementById("imgCareer").src="images/teacher.jpg";
      document.body.innerHTML = document.body.innerHTML.replace('0', '1500');
      break;
  }
}



function startYearOne() {
  console.log("startYear works!");
  document.getElementsByClassName("scrChosenCareer")[0].style.display = "none";
  document.getElementsByClassName("scrControls")[0].style.display = "block";
}
.scrChosenCareer{
  display:none;
}
.scrControls{
  display:none;
}
<div class="scrChosenCareer">
        <h3>Congratulations! You are a profession. You make $0 a year.</h3>
        <img src="images/doctor2.jpg" id="imgCareer"></img>
        <h3>After incurring living expenses of $700 a year,
        you can allocate the rest into:</h3>
        <ul>
          <li>a bank, and gain a 5% annual interest. </li>
          <li>put it in stocks and see how the market performs.</li>
          <li>Any money not allocated, will be placed as cash under your mattress.</li>
        </ul>
        <button id="btnYearOne">Click to continue</button>
    </div>

    <div class="scrControls">
        <input type="range" value="50" min="0" max="100" id="rStocks" oninput="rangevalue.value=value"/></input>
        <input type="number" id="stocksValue" value="50" oninput="range.value=value"></input>

        <input type="range" value="50" min="0" max="100" id="rBank" oninput="rangevalue.value=value"/></input>
        <input type="number" id="bankValue" value="50" oninput="range.value=value"></input>

        <button id="btnContinue">Continue to Next Year</button>
    </div>

Nothing shows up on console either.

Any assistance provided would be greatly appreciated!

Answer №1

Which element has an ID of "btnJobs"? Did you mean "btnContinue"?

Which element has a class-name of "scrJobs"? Did you mean "scrControls"?

document.getElementById("btnContinue").addEventListener("click", funcJobs);
document.getElementById("btnYearOne").addEventListener("click", startYearOne);

function funcJobs() {

document.getElementsByClassName("scrControls")[0].style.display = "none";
  document.getElementsByClassName("scrChosenCareer")[0].style.display = "block";

  var jobList = ["doctor", "engineer", "artist"];

  var randomJob = jobList[Math.floor(Math.random()*jobList.length)];
  document.body.innerHTML = document.body.innerHTML.replace('profession', randomJob);

  switch (randomJob) {
    case 'doctor':
      document.getElementById("imgCareer").src="images/doctor.png";
      document.body.innerHTML = document.body.innerHTML.replace('0', '1300');
      break;
    case 'engineer':
      document.getElementById("imgCareer").src="images/engineer.webp";
      document.body.innerHTML = document.body.innerHTML.replace('0', '1000');
      break;
    case 'artist':
      document.getElementById("imgCareer").src="images/artist.jpg";
      document.body.innerHTML = document.body.innerHTML.replace('0', '1500');
      break;
  }
}



function startYearOne() {
  console.log("startYear works!");
  document.getElementsByClassName("scrChosenCareer")[0].style.display = "none";
  document.getElementsByClassName("scrControls")[0].style.display = "block";
}
.scrChosenCareer{
  /* display:none; */
}
.scrControls{
  display:none;
}
<div class="scrChosenCareer">
        <h3>Congratulations! You are a profession. You make $0 a year.</h3>
        <img src="images/doctor2.jpg" id="imgCareer"></img>
        <h3>After incurring living expenses of $700 a year,
        you can allocate the rest into:</h3>
        <ul>
          <li>a bank, and gain a 5% annual interest. </li>
          <li>put it in stocks and see how the market performs.</li>
          <li>Any money not allocated, will be placed as cash under your mattress.</li>
        </ul>
        <button id="btnYearOne">Click to continue</button>
    </div>

    <div class="scrControls">
        <input type="range" value="50" min="0" max="100" id="rStocks" oninput="rangevalue.value=value"/></input>
        <input type="number" id="stocksValue" value="50" oninput="range.value=value"></input>

        <input type="range" value="50" min="0" max="100" id="rBank" oninput="rangevalue.value=value"/></input>
        <input type="number" id="bankValue" value="50" oninput="range.value=value"></input>

        <button id="btnContinue">Continue to Next Year</button>
    </div>

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

Creating a hierarchical JSON format for a nested commenting system

I have a JSON data representing a multi-level comment system as shown below: [{ "thread_id": 2710, "parent_id": "", "username": "string", "comment": "string", "postdate": "2017-06-09T07:12:32.000Z", "id": 1 }, { "thread_id": 2710, "parent_ ...

Styling Drawn Elements on a Canvas Using CSS

Can CSS Animations be applied to a circle drawn on a canvas using JavaScript? Specifically, I am using an AngularJS directive for this purpose: https://github.com/angular-directives/angular-round-progress-directive/blob/master/angular-round-progress-direct ...

Regular occurrences of heap memory exhaustion within a node.js application running in a docker container

My node.js application consists of 16 microservices, a docker image, and is hosted on the Google Cloud Platform using Kubernetes. However, when handling API requests from just 100 users, some of the main docker images are crashing due to heap out of memor ...

Changing the content of a PHP div with an Ajax callback inside another Ajax callback?

In the index.php file, there is a script that triggers an ajax call when a header element is clicked. This call sends a $selection variable to ajax.php, which then replaces #div1 with the received HTML data. <script> $(document).ready(function(){ ...

"Strategic victory in a board game - employing an advanced search algorithm

Currently seeking an efficient algorithm to detect a "win" situation in a gomoku (five-in-a-row) game played on a 19x19 board. A win occurs when a player successfully aligns five, and ONLY five, "stones" in a row (horizontally, diagonally, or vertically). ...

Creating reusable functions in VueJS that can be accessed globally by all child components

Looking for assistance in setting up a universal function that can be accessed across all my Vue files. For example, when using this code snippet in a Vue file: @click="ModalShow.show('my-create')" I have defined the following constan ...

Understanding the extent of variables in Javascript

I'm struggling to comprehend the scope of 'this' in this particular situation. I can easily call each of these functions like: this.startTracking(); from within the time tracker switch object. However, when attempting to execute the code: Dr ...

Issue with variable creation within ng-repeat

After reading a question on StackOverflow about determining the number of filtered out elements, I wrote the following code in Angular: <input type="text" ng-model="query" placeholder="Enter query..."> <div ng-if="filteredData.length!=data.length ...

Having trouble aligning and resizing text and divs accurately

Hello, I'm having trouble centering my text within a div and fitting three divs inside my content area. Here is the code snippet: HTML <div id="content"> <div class="latest"> <p>Gentlemen, these are my latest works< ...

Button from Bootstrap extends beyond the page on mobile devices

I am facing an issue with my button. It looks great on desktop, but when I switch to the mobile version, it goes beyond the screen width like in the image below: https://i.sstatic.net/69Zbs.png 1) How can I prevent this from happening with the code provi ...

CSS and JavaScript dropdown malfunctioning due to a position swap in internal CSS

This example demonstrates functionality .c1 { background-color:red; position:relative; } .c2 { background-color:blue; position:absolute; height:50px; width:100px; display:none;} .show { display:block; } <body> <button ...

Responsive Input Navigation Bar CSS

How can I create a top bar that resembles the design of Google Play? https://i.sstatic.net/tdLVz.png I am struggling to make the input box flexible and fit the window size... The minimum width of Div.search is set to 250px but it's not working ...

Using jQuery, prevent the user from entering text into an input box when a checkbox

In my project, there is a checkbox that determines whether an input box is disabled. The issue arises when I try to disable the input based on the database value. For example, when the value is 0, it should display as disabled false; however, the input rem ...

Is there a way to link the IDs in the data with Vue.js to access the corresponding names?

In the project I'm currently working on, I have two endpoints that provide different information. The 'categories' endpoint supplies me with category names and their corresponding id values, while the 'products' endpoint only gives ...

Having trouble getting my CSS gradient animation to work

I'm experimenting with creating a dynamic CSS gradient background effect. I'm using a tool to generate the CSS for the gradient, which can be found at this page: Here is the CSS code snippet: body { background: linear-gradient(270deg, #18f0b8, ...

Setting a default value for a pair of buttons in VueJS depending on the API response is a straightforward process

One of the challenges I am facing involves toggling a pair of buttons based on the response received from an API. These buttons need to be set only once when the page initially loads. https://i.sstatic.net/bMgmE.png Below is the code snippet I am current ...

What is the best approach for manipulating live data in localStorage using ReactJS?

I am working on creating a page that dynamically renders data from localStorage in real-time. My goal is to have the UI update instantly when I delete data from localStorage. Currently, my code does not reflect changes in real-time; I have to manually rel ...

How do I switch the background-image on li hover and revert it back when I move off the li element?

Looking to update the background image of a div upon hovering over a li element? Check out this example here. So far, I've got that part working fine. But now I want the background picture to revert back to its original CSS when I move away from the l ...

"Ensuring a fixed table header (thead) with Bootstrap styling in AngularJS: A step-by-step guide

I'm struggling to make the header of my AngularJS webpage sticky using Bootstrap Styling. Despite trying multiple solutions, none seem to work correctly. Does anyone have a simple approach to fix this issue? Additionally, when I attempt to make the he ...

What are some effective tactics for reducers in react and redux?

Working on a React + Redux project to create a web app that communicates with an API, similar to the example provided at https://github.com/reactjs/redux/tree/master/examples/real-world. The API I'm using returns lists of artists, albums, and tracks, ...