What is the best way to utilize jQuery toggle class for adding or removing elements?

For some reason, my code isn't working as expected even though it seems fine to me. I am new to jQuery and attempting to utilize toggleClass to show card information when the card on my page is clicked. Initially, I want the information to be hidden but toggle-able.

$(document).ready(function() {
  $('.card').click(function() {
    $(this).toggleClass('inner-card.active');
  });
});
.card {
  display: flex;
  flex-direction: column;
  justify-content: end;
  width: 350px;
  height: 180px;
  background: lightgreen;
  border: 2px solid black;
  margin-left: 8px;
  margin-bottom: 8px;
  cursor: pointer;
}

.inner-card .active {
  display: none;
}

.inner-card {
  display: flex;
  flex-direction: column;
  justify-content: end;
  background: rgba(255, 165, 0, 0.5)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card">
  <div class="inner-card">
    <h5>Title</h5>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sunt, libero?</p>
    <a href="">Link goes here</a>
    <div class="img-div">
      <img src="../static/img/search.png" class="card-img" alt="">
    </div>
  </div>
</div>

The transition should look like this:

https://i.sstatic.net/CHuVs.png

To achieve something similar to this:

https://i.sstatic.net/jhGrR.png

Answer №1

In my opinion, there are a few issues that need to be addressed. Firstly, when implementing the display:none CSS logic, it should be written as follows:

.inner-card.active{
    display: none;
} 

This indicates that the inner card will be hidden if it also possesses the active class.

Additionally, I recommend modifying the script in the following manner:

 $(document).ready( function(){
     $('.card').click( function() {
         $(this).find(".inner-card").toggleClass('active');
     });
 });

When utilizing the toggleClass method, make sure to only include the class name without the selector (-> no dot). Furthermore, based on the CSS code provided, it seems essential to locate the inner card element initially.

Link to Fiddle

Answer №2

If you're looking for a solution, consider the following:

$(document).ready(function() {
  $('.card').click(function() {
    $(this).find(".inner-card").toggleClass('hidden');
  });
});
.card {
  display: flex;
  flex-direction: column;
  justify-content: end;
  width: 350px;
  height: 180px;
  background: lightgreen;
  border: 2px solid black;
  margin-left: 8px;
  margin-bottom: 8px;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.inner-card {
  display: flex;
  flex-direction: column;
  justify-content: end;
  background: rgba(255, 165, 0, 0.5)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card">
  <div class="inner-card hidden">
    <h5>Title</h5>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sunt, libero?</p>
    <a href="">Link goes here</a>
    <div class="img-div">
      <img src="../static/img/search.png" class="card-img" alt="magnify lens">
    </div>
  </div>
</div>

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

Guide on Modifying the CSS File of an Installed Plugin on WordPress

I am looking to customize the design of a WordPress plugin by editing its CSS file. When trying to locate the CSS file of the installed plugin through Plugins --> Installed Plugin --> Editor, I could only find .php files instead of the desired CSS f ...

Implementing search features with vue.js

I am currently working on implementing a search filter into my application using vue.js. Issue: [Vue warn]: Error in v-on handler: "TypeError: handler.apply is not a function" (found in <Root>) warn @ vue.js:634 HTML: <div id ="search"> &l ...

It's frustrating when Redux triggers unnecessary re-rendering of a component

While working on my website with React and Redux, I encountered an issue regarding the image display within a card component. Initially, I have this card featuring a specific product. https://i.sstatic.net/CEoq7.png The functionality allows me to click on ...

Implementing additional input with Jquery causes tooltips to malfunction

I am developing a form that allows users to add as many rows as needed. Each input is being treated as an array for easy data storage in the database, which is a new concept for me. $(document).ready(function() { var maxField = 10; //Limitati ...

padding applied exclusively to the Bootstrap column when viewed on a large grid

Can padding be added only for the large grid and not for the medium grid? Large grid with padding: <div class="col-lg-4 pl-3"> </div> Medium grid without padding: <div class="col-md-3"> </div> ...

PHP-powered Countdown Timer for triggering a button's action

Hey everyone, I could use some assistance. Here's my situation - I have a database table with a column called "created_time" that stores the current system time in HH:MM AM/PM format. I want to have a button on one of my PHP pages named "Start Exam" t ...

What is the best location to include the sitemap.xml file within a NextJS project?

I recently completed building my NextJS website and utilized an online sitemap generator to create my sitemap.xml file. In the past, when working with ReactJS, I would place it in the public folder, or in the dist folder for projects using regular HTML, CS ...

Stylesheet compilation with SCSS scripts

I am facing an issue with prefix:css. I am not very familiar with the scss build process, so if anyone knows what the problem is, please help me out. "watch:sass": "node-sass src/styles/global.scss src/assets/css/style.css -w", ...

What is the best way to manage the POST method in NextJS?

I am currently delving into the realms of NextJs(TS), Prisma, and MySQL with the aim to implement my learnings in ReactJS. However, I seem to be encountering some difficulties when attempting to make a POST call. Here's an overview of my project dire ...

Swap out the text of one element with a different text content found within the DOM

I have a web app that features a template for displaying a list of books retrieved from the server using Python (Flask). The information about the books, including titles, IDs, and authors' names, is stored in the variable "books" which I then loop ov ...

Node.js - updating the value of a exported integer variable

In my file A.js, I have defined a module-level variable called activeCount and exported it using module.exports. In another file named testA.js, I am attempting to access and modify the value of activeCount. Despite my efforts, changes made to activeCount ...

Margin isn't functioning properly

My section is not centered and adding margin-left doesn't seem to be working. Any suggestions on how I can solve this? Take a look at how it appears. Here's the code: .margins { margin-left: 100px; } <link href="https://cdn.jsdelivr.net ...

When attempting to install material UI in my terminal, I encounter issues and encounter errors along the way

$ npm install @material-ui/core npm version : 6.14.4 Error: Source text contains an unrecognized token. At line:1 char:15 $ npm install <<<< @material-ui/core CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException ...

Retrieve an HTML page rendering using C#

Is there a way to retrieve the exact HTML code from my website just as it appears in the browser? Initially, I attempted to use a web client like this: using (var client = new WebClient()) { var content = client.DownloadString("my_site_address"); } ...

What is the best way to showcase an image using CSS on my personal computer?

It's a bit embarrassing to admit, but I'm learning some new concepts and struggling with this one. On my computer, there's a folder named Test that contains an index.html file, a CSS subfolder with an index.css file, and an images subfolder ...

Adding a border with vertical padding above and below two floated divs with different heights

Here is some sample markup: <div class="container"> <div class="one">column a<br />column a</div> <div class="two">column b</div> </div> The content in the inner divs can vary in height and is dynamically g ...

What is the process for adding extra CSS to a webpage displayed in WebView2?

Currently, I am utilizing the Webview2 control within a winform application. My goal is to enhance the behavior of the loaded page by injecting additional CSS code when a specific URL is being displayed in the browser control. Specifically, I aim to implem ...

Tips for efficiently utilizing both client-server side rendering and static generation rendering in web development

I am looking to implement static generation for top products using getStaticProps. Currently, there are sections in my rendering that do not require static generation, such as comments and related products. Here is the full code snippet: export default fu ...

The functionality of this code is optimal in Chrome and Firefox, yet it encounters issues in IE

Currently, I am attempting to create a jquery tooltip that will pop up when a user hovers over a link. The link in question is set to display:block style in order to cover a larger area. Surprisingly, this setup works flawlessly in Chrome and Firefox, but ...

Don't leap to the top of the page, take your time!

This is the question I have posed <a href="#" onclick="return false">show_all</a> I attempted onclick=preventDefault(); onclick="return false"; However, it is continuing to jump to the top of the page. Any recommendations on how to pr ...