javascript-hide doesn't work, only show

Hello there! I've encountered an issue with a script that I'm running. It is supposed to display the content in the hidden div when clicked, but for some reason, only the "show" element is working and not the "hide" element.

<script type="text/javascript">
    $(function() {
    $(".slidingDiv").hide();
    $(".hide, .show").show().on('click', function(e) {
        var elm = $(e.target).is('.show') ? $(e.target) : $(e.target).parent().prev('.show');
        elm.toggle().next(".slidingDiv").slideToggle()
    });
});
</script>

<style type="text/css">
.slidingDiv {
    display: none;
    height:200px;
    width:auto;
}
</style

<div id="footer-widgets-container">
    <div id="footer-widgets" class="col-full col-4"> <a href="#" class="show">Show</a>

        <div class="slidingDiv">
            <div class="block footer-widget-1 widget">
                <div class="col1a"></div>
                <div class="col3c"></div>
                <div class="col2b"></div>
            </div>
            <div class="block footer-widget-2"></div>
            <div class="block footer-widget-3"></div>
            <div class="block footer-widget-4">
                <div class="skype p"><a href="#"><p>Skype</p></a>
                </div>
                <div class="email p"><a href="#"><p>Email</p></a>
                </div>
            </div>
        </div>
    </div><a href="#" class="hide">Hide</a>

</div>

Answer №1

It appears that the issue lies with the selector you are using.

To rectify this, you have two options: either move your hide tag so that it is within the correct div, or adjust your code as shown below:

<script type="text/javascript">
    $(function() {
        $(".slidingDiv").hide();
        $(".hide, .show").show().on('click', function(e) {
            var elm = $(e.target).is('.show') ? $(e.target) : $(e.target).parent().find('.show');
            elm.toggle().next(".slidingDiv").slideToggle()
        });
    });
</script>

I modified

$(e.target).parent().prev('. show')
to
$(e.target).parent().find('.show')
since it seems that the show element is not located within that particular div.

This adjustment should resolve the issue for you. Feel free to reach out if you require further assistance.

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 gain entry to this array in vuejs?

Can anyone help me with accessing the objects in this array? I am using laravel, inertiajs, and vuejs. I am passing a variable from a laravel controller to a vuejs component with inertia.js. https://i.stack.imgur.com/p7yjL.png https://i.stack.imgur.com/y ...

Can a shape similar to an inverted circle be created using CSS and jQuery?

I'm stumped on how to create an inverted circle in the corners. I've included a picture for reference. https://i.stack.imgur.com/jeNdh.jpg Is it feasible to achieve this effect using CSS3 or maybe jQuery? ...

What are some ways to prevent popups from being hidden by CSS?

I encountered a problem with my popup that I created using pure CSS. When I move my mouse to the top or bottom of the window, the popup disappears. However, if my cursor is in the body area, the popup appears as intended. I have not used any JavaScript in ...

The Three JS library seems to be malfunctioning as it is only displaying a blank, black screen with no

After browsing through similar questions on various sites, I have attempted all the recommended suggestions. From adjusting camera positioning to calling the render function and setting ambient light, I've tried everything mentioned in forums but noth ...

Integrate a feature in your form that allows users to preview and edit their submission before finalizing

I've set up my HTML form with the following structure: <div class="container"> <div class="form-group" ng-controller="studentController"> <form role="form" class="well form-horizontal" id="registerForm" name="forms.register ...

What is the best way to integrate an image gallery with twitter bootstrap?

I'm having trouble implementing an image gallery on my index page. Despite my efforts, I can't seem to achieve the desired behavior. Here's a snippet of my code: .container .row .col-md-8.col-sm-8.col-xs-8 - 4.times do |i| ...

I'm curious as to why window.opener is null in a popup when utilizing Google OAuth, and what is the most effective way to transfer the access token to the parent window

In my React application, I am working with Google OAuth and implementing it through a popup. The setup involves using Passport with my own custom backend. I start by supplying the initial URL to the popup window, which is the entry point on my backend that ...

Designing access to data in JavaScript

As I work on developing my single page app, I find myself diving into the data access layer for the client-side. In this process, a question arises regarding the optimal design approach. While I am aware that JavaScript is callback-centric, I can't he ...

Showing information from a database while incorporating line breaks with the help of nl2br

Having trouble with the previous question, so I'll provide more detail here. Below is my index.php file where I explain the method used to save data to a database. <html> <head> <script> function updategroup() { var update_c ...

Is it possible for the relative path of a gif image to become invalid when added to a div?

Question Context: In the view, I have a form that will show a loading 'spinner' gif when a user submits it. The Problem: If I place the spinner img element within its container div, the spinner is always displayed, which is not desired: https ...

Establishing the folder organization for Express Handlebars

I work with NodeJs, Express, and Handlebars. The main file for my server is named app.js const express = require('express'); const exphbs = require('express-handlebars'); const app = express(); app.engine('handlebars', ex ...

I consistently encounter the error message 'XRWebGLLayer is not defined no-undef' while implementing three.js within react.js to develop a WebXR application

Trying to implement WebXR with Three.js in a React project, encountering the error: 'XRWebGLLayer' is not defined no-undef for baseLayer: new XRWebGLLayer(session, gl) The code works fine in vanilla JavaScript but fails to compile in React. ...

Decoding json data with targeted API keys

Looking for some assistance here. I've got a JSON data set that looks like this: [ { "positive": 2, "negative": 4 }, { "positive": 9, "negative": 18 }, { "positive": 6, "negative": 12 } ...

Having difficulty configuring Compass in WebStorm

Trying to integrate Compass into an existing WebStorm project has been challenging for me as the CSS files are not linking properly. Despite my efforts to modify the relative links, I have not yet managed to resolve the issue. The folders and config.rb we ...

Why isn't my div displaying in JavaScript?

Currently working on a project for the Odin Project front-end web development. My task involves creating a grid using javascript/jQuery. I attempted to use the createElement method but have encountered an issue in making the div visible within the html. Am ...

Issues arise after the update of Chrome causing JavaScript to malfunction

Often, I find myself accidentally closing my Chrome browser and having to go through the tedious process of reopening and reloading all the tabs I had been working on. Frustrated with Chrome's lack of a built-in confirmation mechanism before closing, ...

AJAX functions are only executed during the initial loading of a page

I am facing an issue with my website where dynamically created URLs are not refreshing the page data on the second click. The initial click triggers an ajax query to load new data successfully, but subsequent clicks do not execute the query. Below is the ...

Learn how to efficiently pass multiple props using a loop in Vue

I am dealing with an object that has multiple properties. Typically, I know which props I want to pass to my component and do it like this: <component :prop1="object.prop1" :prop2="object.prop2" :prop3="object.prop3" /> However, I want to pass the ...

How can the total price for a shopping cart be computed in React/Redux?

After extensive searches on Google and SO, I'm still coming up empty-handed when it comes to calculating the total price of items in a cart. Many tutorials stop at basic cart functionalities like "Add item to cart" or "increase/decrease quantity", but ...

Insert HTML elements into nested CSS classes

My goal is to utilize jQuery for looping through classes and adding text to an HTML element. I am currently working with the following example HTML: <div class="question"> <div class="title"> <strong>Here's the question ...