Flaw in Basic Function Logic Using HTML, JavaScript, and CSS

Need some help with the function onBall3Click1 (code is at the bottom).

The ball should act like a lightbulb: ON - turn YELLOW, OFF - turn GRAY.

I've been trying to figure out the logic behind it for so long and can't seem to find the issue...

Even though the text changes to 'ON' when clicked, the color stays gray instead of turning yellow as expected.

Your assistance is greatly appreciated!

<html>

<head>

  <style>
    body {
      background-color: black;
      text-align: center;
    }
    
    h1 {
      color: white;
    }
    
    div {
      width: 100px;
      height: 100px;
      margin: auto;
      margin-bottom: 10px;
      border-radius: 50%;
      transition: 0.3s;
      line-height: 50px;
      color: black
    }
    
    .ball1 {
      background-color: yellow;
      border: 6px solid gray;
      color: black
    }
    
    .ball2 {
      background-color: orange;
    }
    
    .ball3 {
      background-color: gray;
    }
    
    .ball4 {
      background-color: brown;
    }
  </style>
</head>

<body>
  <h1>The Ball</h1>

  <div class="ball1" onclick="onBall1Click()">
    GROW 250 ORANGE
  </div>
  <div class="ball2" onclick="onBall2Click()">
    GROW+50 SHRINK-50
  </div>
  <div class="ball3" onclick="onBall3Click1()">
    OFF
  </div>
  <div class="ball4" onclick="onBall4Click()">
    PROMPT
  </div>

  <script>
    var ball1Size = 100;
    var ball1SizeStep = 50;

    function onBall1Click() {
      var ball1 = document.querySelector('.ball1');
      ball1Size = ball1Size + 50;

      if (ball1Size > 400) {
        ball1Size = 100;
      }
      ball1.innerText = ball1Size;
      ball1.style.width = ball1Size;
      ball1.style.height = ball1Size;

      ball1.innerText = ball1Size;
      if (ball1Size == 250) {
        ball1.style.color = 'Orange';
      } else {
        ball1.style.color = 'black'
      }


    }

    var ball2Size = 100;
    var ball2SizeStep = 50;

    function onBall2Click() {
      var ball2 = document.querySelector('.ball2');
      ball2Size += ball2SizeStep;

      if (ball2Size === 400) {
        ball2SizeStep = -50;
      } else if (ball2Size === 100) {
        ball2SizeStep = 50;
      }
      if (ball2Size > 100) {
        ball2.style.backgroundColor = 'red';
      } else if (ball2Size === 100) {
        ball2.style.backgroundColor = 'Orange';
      }



      ball2.innerText = ball2Size;
      ball2.style.width = ball2Size;
      ball2.style.height = ball2Size;


    }

    function onBall3Click1() {
      var ball3 = document.querySelector('.ball3');
      console.log("Hi there");
      if (ball3.innerText == 'OFF') {
        ball3.innerText = 'ON';
      } else if (ball3.innerText == 'ON') {
        ball3.innerText = 'OFF'
      }
      console.log(ball3.style.backgroundColor)
      if (ball3.style.backgroundColor === 'gray') {
        console.log("Color change happening now...");
        ball3.style.backgroundColor = 'Yellow';
      } else if (ball3.style.backgroundColor == 'Yellow') {
        console.log("Another color change in progress...");
        ball3.style.backgroundColor = 'gray'
      }

    }

    var ball4Size = 100;

    function onBall4Click() {
      var ball4 = document.querySelector('.ball4');
      var user_input = prompt('Enter the size number for ball 4 but keep it reasonable :   ')
      let user_input_int = parseInt(user_input)
      console.log(user_input_int);
      if (user_input_int < 1000) {
        ball4.style.width = user_input_int;
        ball4.style.height = user_input_int;
      } else {
        alert("That's too big!");
      }
    }
  </script>
</body>

</html>

Answer №1

To streamline the onBall3Click1 function, you can combine the if/elseif statements like this:

    function onBall3Click1() {
        var ball3 = document.querySelector('.ball3');
        console.log("HELLO");
        if (ball3.innerText == 'OFF') {
            ball3.innerText = 'ON';
            ball3.style.backgroundColor = 'yellow';
        } else if (ball3.innerText == 'ON') {
            ball3.innerText = 'OFF'
            ball3.style.backgroundColor = 'gray'
        }
        console.log(ball3.style.backgroundColor)

    }

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

Is there a way for me to view the output of my TypeScript code in an HTML document?

This is my HTML *all the code has been modified <div class="testCenter"> <h1>{{changed()}}</h1> </div> This is my .ts code I am unsure about the functionality of the changed() function import { Component, OnInit } f ...

The problem of interpreting JSON using the JavaScript eval() function

Here is the JSON string provided : { "name":"Ruby on Rails Baseball Jersey", "price":"19.99", "id":"1025786064", "image":"" }, { "name":"Ruby on Rails Baseball Jersey", "price":"19.99", "id":"1025786064", "image":"" }, { "name ...

Is it possible to customize the color of the modal backdrop in layer v4 of the Material-UI library

I am struggling to modify the background color of an MUI V4 modal. Instead of getting a clean color, I am seeing a gray layer on top of the backdrop. Though this seems to be the default behavior, I want to remove it and target the shaded div directly rathe ...

Injecting components into the DOM using JavaScript in Vue.js

I am currently developing a GUI for a webgame using Vue.JS and opting to use http-vue-loader instead of Webpack or similar tools. Personally, I find them cumbersome and prefer to avoid using them. Typically, in order to integrate my components into the DO ...

PageCallbacks in .NET 1.1

Is it possible to utilize PageMethods in .NET 1.1 for making server-side method calls from the client side? Could you provide a brief explanation on how to use this feature by calling PageMethods.MyMethod(); ? ...

Using CSS to showcase div elements with varying dimensions

How can I arrange divs with varying heights to be positioned close together, regardless of their height, and with a specific margin similar to buildings? I am aiming for a layout like the one on this website . I tried using float:left but it only float ...

Why is it important to understand the meaning of variable = [...variable] in Javascript ES6?

I need clarification on the following variable arg assignment arg = [...arg]; Can you explain what this code snippet does? ...

Using the onclick attribute as a unique identifier for a button

I am currently facing a challenge with a form that does not have an ID Here is the code snippet in question: <button class="btn btn-primary" onclick="showModal()" type="button">Browse Data</button> Unfortunately, I don't have any contro ...

Click event in jQuery to change the background color of <td> element

I'm still getting the hang of jQuery, so please bear with me. :) The purpose of this code is to color a square with the selected color when clicked if it's white, and turn it back to white if it's already colored. It works fine except for a ...

What is the reason behind an Express function requiring both the Request and Response as parameters?

Exploring the world of node.js and familiarizing myself with Express. The code snippet below has left me puzzled: var server = http.createServer(handleRequest); function handleRequest(req, res) { var path = req.url; switch (path) { case "/n": ...

When working on a Vue project, the Navbar @click functionality seems to

Having trouble with my navbar search form <form action="" method="post" class="search"> <input type="text" name="" placeholder="поиск" class="input" v-model="alls ...

When using jQuery, adding a class does not always trigger CSS animation

Currently facing a peculiar issue. I am utilizing jQuery to load articles from JSON and would like to dynamically add a class of 'animate' to each loaded element. $.each(jsonArticles, function (i, article) { var $articleHTML = $( ' ...

Hiding or removing DOM elements with ng-bootstrap pagination

I am in the process of incorporating pagination into my project using ng-bootstrap pagination. In my HTML, I am combining an ngFor loop with the slice pipe to filter elements for display. <tr *ngFor="let bankAccount of bankingAccounts | slice: (p ...

What is the best way to make text appear as if it is floating in Jade or HTML?

Currently, I am facing an issue with a Jade file that displays an error message when a user tries to log in with incorrect credentials. The main problem is that this error message disrupts the alignment of everything else on the page, as it is just a regul ...

Having trouble with Javascript not detecting when it's empty?

Recently, I have been attempting to modify the color of a submit button when a form is empty. As a beginner in this area, I am somewhat puzzled as to what mistake I might be making. I will share the current code with you in hopes of receiving some guidance ...

The error message "Potree-Core: THREE is undefined" indicates that the

I'm currently developing a web-based application that can be used offline by other users on their local machines. Within my NPM project, I am utilizing Potree-Core, which relies on THREE.js. However, the source code does not include an import for THR ...

`18n implementation in Node.js and front-end JavaScript for localization`

I am currently utilizing express js 4.0 and have set up the i18n module from https://github.com/mashpie/i18n-node in combination with ejs views. My goal is to extend this i18n functionality to .js files as well. Is there a way to enable the i18n function ...

What significance does comparing two arrays hold in the realm of Javascript?

While working in my node.js REPL, I have created 4 arrays: a = [1,2,3], b=[], c=[4,5], d=null (although d is not actually an array). I decided to compare them directly like this: > b = [] [] > a > b true > b > a false > a > c false & ...

Generating dynamic slots in VueJS allows for the creation of

Creating slots dynamically from an array is my current task. After some tinkering, I've managed to make it work using the following code snippet: <template v-for="(department,id) in departments" v-slot:[id]="record"> < ...

Enhance the appearance of QTreeView items using a customized display delegate and CSS

In my QTreeView, I have a custom delegate set up for one of the columns to display a colored progress bar. The appearance of the progress bar depends on the content and I used the color information from option.palette to mimic the default delegate behavior ...