utilizing the removeClass method to toggle the visibility of a div by removing the "hidden" class

I am currently developing a dynamic game utilizing HTML, CSS, and jQuery. In the lower right corner of the screen, there is a question mark (which is an image file, not text). My objective is to display a help box when the question mark is clicked and held down. The box should disappear once the mouse is released. There are three distinct help boxes for each of the three pages.

The question mark image functions as the help button

.help-box represents all the help boxes across different pages: .growing-plants, .label-flower, and .types-animal.

This is my code:

script.js file:

$('#help').mousedown(function() {
  if (currentSection == 1) {
    $('.growing-plants').removeClass("hidden");
  } else if (currentSection == 2) {
    $('.label-flower').removeClass("hidden");
  } else if (currentSection == 3) {
    $('.types-animal').removeClass("hidden");
  }
});

$('#help').mouseup(function() {
  if (currentSection == 1) {
      $('.growing-plants').addClass("hidden");
  } else if (currentSection == 2) {
      $('.label-flower').addClass("hidden");
  } else if (currentSection == 3) {
      $('.types-animal').addClass("hidden");
  }

});

style.css file:

#help {
     position: absolute;
     left:900px;
}

.help-box {
     position: absolute;
     top: 200px;
     left: 220px;
     z-index: 15;
}

.growing-plants {
     overflow: hidden;
}

.label-flower {
     overflow: hidden;
}

.types-animal {
     overflow: hidden;
}

html file:

<img src="images/help-growing-plants.png" class="help-box growing-plants" alt="help" width="600" height="400">
<img src="images/help-label-flower.png" class="help-box label-flower" alt="help" width="600" height="400">
<img src="images/help-types-animal.png" class="help-box label-flower" alt="help" width="600" height="400">

Any assistance or recommendations would be greatly appreciated!!!!

Answer №1

To start, make sure to hide your elements by utilizing the CSS property display: none;

.help-box {
     position: absolute;
     top: 200px;
     left: 220px;
     z-index: 15;
     display: none;
}

Afterwards, you can employ jQuery's .show() and .hide() functions

$('#help').mousedown(function() {
  if (currentSection == 1) {
    $('.growing-plants').show();
  } else if (currentSection == 2) {
    $('.label-flower').show();
  } else if (currentSection == 3) {
    $('.types-animal').show();
  }
});

$('#help').mouseup(function() {
  if (currentSection == 1) {
      $('.growing-plants').hide();
  } else if (currentSection == 2) {
      $('.label-flower').hide();
  } else if (currentSection == 3) {
      $('.types-animal').hide();
  }

Answer №2

If your "hidden" class is defined in a different part of the code that is not linked, the issue arises when trying to use .addClass(classNameYouWantToAdd) or

.removeClass(classNameYouWantToAdd)
which are searching for a "hidden" class that has not been defined.

The solution would be to define a class named "hidden" and apply it to the div containing the content you wish to conceal:

.hidden {
    visibility:hidden;
}

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 sidebar navigation in HTML and CSS to easily navigate to specific sections on a webpage

Here's a visual representation of my question I'm interested in creating a feature where scrolling will display dots indicating the current position on the page, allowing users to click on them to navigate to specific sections. How can I achieve ...

The rendering of the font appears distinct when viewed on an iPad compared to when displayed

Visit this website. The menu displays correctly on desktop, but on iPads, the font appears larger leading to a line break. Is it because the selected font isn't loading and defaulting to Times New Roman instead? It seems likely since I experience the ...

The HTML status code is 200, even though the JQuery ajax request shows a status code of 0

My issue is not related to cross site request problem, which is a common suggestion in search results for similar questions. When attempting to make an ajax request using jquery functions .get and .load, I'm receiving xhr.status 0 and xhr.statusText ...

Tips on increasing video size upon visiting a website. HTML, JavaScript, and potentially jQuery can be used for video animation

Is there a way to create a video pop-up in the same window when visiting a website? I want the video to increase in height from 0 to a specific height and move slightly upwards. Are there JavaScript or jQuery functions that can achieve this effect? I wou ...

Utilize CSS to position an image in four various locations on a web page

Expressing this may be a challenge, but I'll give it a shot. Within a div, there is text that can vary in length based on user input. My goal is to have the ability to insert an image in one of several positions, determined by the selection made in a ...

Sending information from MVC controller back to the originating view

Being relatively new to asp.net MVC, I have encountered a challenge that I hope someone can assist me with. My issue revolves around a text box where users input search terms. Upon clicking the search button, an Ajax call is made to pass the textbox value ...

I need to display 5 columns in a parent component, each with its own unique icon. How can I conditionally render them in a React component?

Creating a parent component to reuse multiple times can be very useful. In my case, I have included 5 different icons in each instance of the component, all taken from hero icons. I have set up an object with unique ids for each child component, but I am ...

Organizing a list based on the text within span elements using either jQuery or underscore

I'm a bit confused about how to arrange an unordered list by the content of the span within each list item. Here is my HTML code: <ul id="appsList"> <li><span>aa</span> <span class="sort">androi ...

Clear out the classes of the other children within the identical parent division

I'm currently in the process of replacing my radio circles with div elements. I've successfully implemented the functionality for selecting options by clicking on the divs, as well as highlighting the selected div. However, I'm facing trou ...

The embedded iframe on YouTube is displaying the incorrect video

I am currently designing a website and I want to feature a video on the homepage. The video is hosted on YouTube and I need to embed it into my website. <html> <iframe width="560" height="315" src="https://www.youtube.com/embed/spPdelVEs_k?rel= ...

Is there a way to prevent Chrome from highlighting text that matches the content of a `position: fixed` element?

I currently have a Toc element in my HTML with the property position: fixed. (This means it remains in place as you scroll) My goal is to prevent Chrome's Find Text feature from recognizing text within that Toc element. (Ideally using JavaScript) ...

Creating a visually appealing layout by dividing HTML content into two columns within a WebView

Utilizing WebView in my application to display html content for reading ebooks presented a challenge during development. After parsing the ebook and converting it into an html string, I load this string into the WebView component. myView.setVerticalScro ...

Troubleshooting: Issues with Ajax Request in Laravel

Attempting to use ajax with Laravel backend for a basic return and display of user information. Utilizing the latest versions of JQuery, Laravel, and MySQL. See below for the simple route: Route::get('users', function() { $users = User::all( ...

Using CSS and JavaScript to hide a div element

In my one-page website, I have a fixed side navigation bar within a div that is initially hidden using the display:none property. Is there a way to make this side nav appear when the user scrolls to a specific section of the page, like when reaching the # ...

JavaScript JCrop feature that allows users to resize images without cropping

I'm currently attempting to utilize JCrop for image cropping, but I'm running into frustratingly incorrect results without understanding why. The process involves an image uploader where selecting an image triggers a JavaScript function that upda ...

When viewing HTML "Div" on smaller screens, the full height may not be displayed properly

My setup includes two monitors: one is 24" and the other is my laptop's 12.5" screen. I have a red box (div) in my web application that displays full height on the larger monitor, but only partially on the smaller one. I'm puzzled as to why it s ...

Customizing the Twitter bootstrap navigation in Wordpress

I've been working on theming my navigation menu using Twitter Bootstrap. I want it to resemble the demo example of a fixed top banner, which can be seen here: http://getbootstrap.com/examples/navbar-fixed-top/ Although I have managed to get it to wor ...

Events related to SVG elements

I have a unique situation where a div containing multiple SVG elements that act as buttons is added to the page after the user interacts with it. I am trying to add events to these SVG elements using event delegation, where I inspect the target of the even ...

Is there a way for me to view the data transmitted by JQuery to php?

I have this piece of jQuery code. I am curious to see what PHP will output. How can I display that result? <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js"></script> </head> <script> $.ajax({ ...

Animating the Click Event to Change Grid Layout in React

Can a grid layout change be animated on click in React? For instance, consider the following component: import { Box, Button, styled, useMediaQuery } from "@mui/material"; import Row1 from "./Row1"; import React from "react"; ...