When I click the jQuery button labeled "open" a second time, it fails to respond

I've been struggling to create a sidebar that toggles visibility with a button click using jQuery. However, for some reason, my div ".sidebarBtn2" is unresponsive when clicked. Any suggestions on what might be causing this issue?

CSS:

.sidebar {
    width: 200px;
    height: 800px;
    background: red;
    border: 1px solid black;
    position: absolute;
    right: 0;
}

.sidebarBtn {
    text-align: center;
    height: 80px;
    width: 20px;
    border-top-left-radius: 30%;
    border-bottom-left-radius: 30%;
    background: orange;
    position: absolute;
    right: 198px;
    top: 328px;
}

.sidebarBtn2:hover,
.sidebarBtn:hover {
    cursor: pointer;
}

.sidebarBtn2 {
    text-align: center;
    height: 80px;
    width: 20px;
    border-top-left-radius: 30%;
    border-bottom-left-radius: 30%;
    background: orange;
    position: absolute;
    right: 0px;
    top: 328px; 
}

HTML:

<!DOCTYPE html>
<html>
<head>
    <link rel='stylesheet' type='text/css' href='stylesheet.css'/>
</head>
<body>
    <div class="sidebarBtn">
        C</br>l</br>o</br>s</br>e</br>
    </div>
    <div class="sidebar"></div>
    <div class="sidebarBtn2">
        O</br>p</br>e</br>n</br>
    </div>

    <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
    <script src="script.js"></script>
</body>
</html>

Javascript:

$(document).ready(function() {
    $(".sidebarBtn2").hide();
    
    $(".sidebarBtn").click(function() {
            $(".sidebarBtn").hide();
            $(".sidebar").hide();
        $(".sidebarBtn2").show();
    });
    
    $(".sidebarBtn2").click(function() {
        $("sidebarBtn2").hide();
        $("sidebar").show();
        $("sidebarBtn").show();
    });
});

Answer №1

Ensure to include the . before the class names for button 2 in your code. Here's a revised version for you:

$(".sidebarBtn2").click(function() {
    $(".sidebarBtn2").hide();
    $(".sidebar").show();
    $(".sidebarBtn").show();
});

Answer №2

Give this a shot:

$(".sidebarBtn2").click(function() {

    // Alternatively, you could use $(this).hide(); instead of $(".sidebarBtn2").hide();
    $(".sidebarBtn2").hide();       

    $(".sidebar").show();
    $(".sidebarBtn").show();
});

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

What is the most recommended jQuery version to utilize?

My goal is to change a selected button (buttons = .answerBtns) to an unselected button whenever the option in the OptionDropId drop down menu changes. Below is the jQuery code for drop down menus: $(document).ready(function () { var OptDrop = new Arr ...

Toggle Jquery menu with a click of a button

I need help with creating a menu for a forum that opens on click and closes on click. Here is the code I have so far: /*Custom BBPress admin links menu*/ function wpmudev_bbp_admin_links_in_menu($retval, $r, $args) { if ( is_user_logged_in() ) { $me ...

Tips for regularly retrieving information from a psql table

I have a scenario where I am retrieving data from a psql table and converting it into a JSON array to be used for displaying a time series chart using JavaScript. The data that is passed needs to be in the form of an array. Since the data in the table get ...

Endless Loop Seamless Vertical JavaScript Experience

I've been working with an HTML table structure of data and was successful in setting up a vertical list loop using some JavaScript. However, I'm facing challenges in achieving a smooth constant vertical scroll. Currently, it goes row by row when ...

Unable to delete cookies that have been created through PHP

Let me explain how I handle cookies using PHP and Javascript. Firstly, in PHP, I create a cookie before the page is loaded using the following code: setcookie('my_key', $value, 0, ADMIN_COOKIE_PATH); For the Javascript part, I utilize jQuery a ...

Whenever the [required] tag switches from being true to false, it generates an event

Initially, I have set up an Angular form using the following code snippet buildForm() { this.form = this.formBuilder.group({ value1: this.formBuilder.control(null), value2: this.formBuilder.control(false) }); } The HTML contains a ...

How to properly align the content inside a div in a Django project

As I take on my first project in Django, I find myself grappling with how to center elements on my site. Specifically, I aim to create a div that encompasses all the content on the page, excluding the navigation bar. This div should be positioned perfectly ...

High-resolution custom cursor in CSS for improved display clarity

I'm having trouble finding information on this particular subject. My goal is to create a simple custom cursor using CSS. Currently, I have the following code: cursor: url('img/cursor_left.png'), auto; The cursor appears as expected, but i ...

Prevent clients from navigating the block by using AngularJS when they enter a URL in the

Is there a way to prevent navigation when the user types /pricing in the address bar, and only allow navigation when $location.path("/pricing") is triggered? when("/pricing", { templateUrl: "app/components/Pricing/pricing.html", }) I'm l ...

"Utilizing the link_to method in Ruby on Rails to track click

I have 3 links with images. Each time a link is clicked, I want to increase my count by 1. <a href="/home" id="push"><img src="/images/image1.gif" /></a> <a href="/faq"> <img src="/images/image2.gif" /></a> <a href=" ...

Adding negative values to the Tailwind CSS utility plugin is a simple process that can greatly enhance

Adding Negative Values to Tailwind CSS Utility Plugin Quick Summary: I've developed a custom Tailwind utility plugin that includes numeric values. I'm looking for a way to introduce negative numbers by adding a - at the start of the class, simi ...

Ways to customize the bootstrap-datetimepicker to display time in 15-minute increments

Here is my code in JavaScript to increment by minutes at a 1-hour interval: fillMinutes = function () { var table = widget.find('.timepicker-minutes table'), currentMinute = viewDate.clone().startOf('h'), ...

Start a new typescript project from scratch

Seeking assistance in setting up a blank TypeScript project with a package.json, TypeScript and HTML file. I am looking for something akin to the Stackblitz blank-typescript project. If anyone could provide me with a step-by-step guide on how to create su ...

Adding text after a div in React-JS using Bootstrap: A quick guide

Just starting out with website development and I have a question. As I practice making this website, I am struggling to figure out how to add the text "To know more about us click here" below the 'Get started' button. I tried adding a simple < ...

Increasing counter on the backend using PHP or JavaScript

Having done my research, I am struggling to find a suitable solution for my project. I need a server-side incremental counter that resets daily. The goal is for every website visitor to see the same number on the counter. Is there a PHP script or JavaScr ...

intricate pattern matching tool for Angular

Can someone help me create a regular expression that can validate input in the format "S19/999999/090"? I need the input field to accept this specific type of input where the first character is an uppercase letter from A to Z, followed by two digits from 0 ...

NodeJS seems to be having trouble with external CSS files

My nodeJS project uses an external CSS file to style the HTML page. Here is how my file structure looks: ProjectFolder ----server.js ----index.html ----style.css This is how my server.js file looks like: var http = require('http'); var f ...

Which is quicker: loading JSON via Ajax or loading the entire output through Ajax?

I'm interested in gathering different perspectives on this topic. Currently, I have Jquery initiating a function through ajax which loads data in two ways: The ajax script fetches JSON data from the server itself, then utilizes JavaScript to pars ...

Utilizing Selenium to scrape various URLs for similar content using subtly different XPaths

Utilizing Selenium to retrieve data from various URLs, each containing similar tables but with slightly different XPaths. See below for my code snippet: my_urls = ["https://www.sec.gov/cgi-bin/own-disp?action=getowner&CIK=0001548760", "https://www.sec.gov ...

Unlock the Power of jQuery Plugins: Implementing Global Settings Modifications

I have developed a jQuery plugin ( https://github.com/OscarGodson/jKey ) and some users are requesting localization support. My initial idea was to add an additional parameter in the plugin for localization, such as: $(window).jkey('?',callback, ...