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

When making an ajax request in Codeigniter, the form validation may return false

Greetings, I am in the process of creating a website using CI on Backend and I'm facing an issue with implementing a registration system without refreshing the page. When I use a post form submit, everything works fine with no errors. However, when at ...

Can we incorporate various CSS libraries for individual components on our React site?

Let's say, I want to use different CSS libraries for each of my components - Home, About, Contact. Would it be feasible to utilize material ui for Home, semantic ui for About, and bootstrap for Contact? If so, what is the process for incorporating t ...

Navigate through a list of data in JSON format

After successfully implementing a jQuery AJAX call, I encountered difficulty in parsing the returned value. Working with a MySQL database, I am returning a PHP array() to my jQuery AJAX function using echo json_encode($reservationArray); Upon appending th ...

What is the best way to showcase HTML content in columns with a horizontal scrolling feature?

I am trying to showcase a list of basic HTML elements such as divs, paragraphs, etc. within a fixed height container, arranged in columns with consistent width. I want them to be horizontally scrollable, similar to the layout displayed in this image. Thi ...

The function send_filer either returns None or finishes execution without encountering a return statement

I am currently developing an application where users can download a plot.png and a file.csv, but the send_files function isn't working as expected. Below is my Python code: app = Flask(__name__) app.USE_X_SENDFILE = True @app.route('/', met ...

Submit an Ajax form to process data in PHP upon selecting a radio button

I am troubleshooting an issue with a form that is not submitting data to processor.php for processing and storing responses in a database. Ensuring that the form submission does not cause a page refresh is crucial, as there is an iframe below the form tha ...

HTML Canvas resizing challenge

My goal is to resize the canvas to fit inside its parent div within a Bootstrap grid, but I'm running into an issue where the canvas keeps expanding to 2000px x 2000px. Despite successfully resizing based on console.log outputs showing the same dimens ...

Incorporating a unique BRAND logo as the header of an HTML page

https://i.sstatic.net/NV5BO.jpg Currently enhancing my front end development skills with HTML and CSS. I am trying to set up a menu bar with an image (header image) above it. However, I am facing issues in displaying the image stored in my images folder. ...

Is it possible to remove a record using jQuery AJAX from a table that has rows dynamically added using AJAX?

I am encountering an issue where I am unable to access or select the delete buttons in my table rows, which are populated within the table body using ajax on document load. It seems that this problem is arising due to the asynchronous nature of ajax. I a ...

JQuery Challenge: Solving Dynamic Modal Issues

I'm in the process of creating a webpage that features multiple divs, each with its own unique image and title. Within each div, there's a button that, when clicked, should grab the specific image and title and display them in a modal. However, I ...

Bootstrap not being recognized in AngularJS HTML

I've been working on learning AngularJS, but unfortunately I can't seem to get any Bootstrap themes to show up in my web application. Here is the HTML code for the header that I've been trying: <nav class="navbar navbar-default"> ...

It is not possible to trigger an input click programmatically on iOS versions older than 12

Encountering a challenge in triggering the opening of a file dialogue on older iOS devices, particularly those running iOS 12. The approach involves utilizing the React-Dropzone package to establish a dropzone for files with an added functionality to tap ...

The use of Jquery's $.ajax function is causing an internal server error

I can't seem to figure out what I'm doing wrong in this code snippet. <script type="text/javascript"> $(function () { $("li").bind("click", function () { var sel = $(this).attr('id').toString ...

Utilizing absolute positioning with a rotated div element

The situation I am facing involves a parent div and child div. The child div is positioned absolutely with respect to the parent div and has been rotated by an angle of 90 degrees. Due to the fact that the origin is located at the center of the child div ( ...

Display partials with ajax in Rails 3/jquery tabs

I am currently working on setting up a simple 3 tab navigation for my post#show page using Rails 3 and jquery. The goal is to render _post, _comments, and _related partials into the view when each tab is clicked. Currently, these partials do not have thei ...

The video embedded from Vimeo fails to play when attempting to start it

There is an overlay play button on the images on my website. When clicked, a Vimeo video should load and automatically start playing. However, I am experiencing an issue where the video loads but pauses automatically, even though the autoplay parameter is ...

Add a div element to the respective list item when clicked

I am looking to display and add a div by clicking on an unordered list that is initially hidden. HTML Code <ul> <li><a href="#">Link1</a></li> <li><a href="#">Link2</a></li> </ul> <d ...

Bootswatch alternative for Bootstrap CSS without utilizing Google Fonts

Currently, I am in the process of developing a webpage and utilizing Bootswatch for styling. However, there are times when I need to work offline and host locally. The issue I am facing is that Bootswatch cannot be used offline due to the fact that it util ...

Reasons for dividing by 1 in JavaScript

While completing a basic programming assignment from my teacher, I encountered an interesting issue in Javascript. I found that when dividing a number by 1, it returns an unexpected value. Can anyone provide an explanation for this? I have created a jsfidd ...

The alignment of margins appears as intended in Opera, Chrome, and IE, but unfortunately it does not

I have exhausted all possible options, but I am still struggling to get this page to appear correctly in Firefox. When you click inside the square, a menu with images should display on the right side. It is properly aligned in every browser except for Fire ...