Display elements exclusively when the class XY is in an active state using JavaScript

Hello everyone, I'm new to this platform and excited to share my first post.

Currently, I find myself facing a major challenge as I navigate through a bootcamp program. I have been working on a website with different sections that require specific functionalities.

const bookmarkButton = document.querySelectorAll('.pictureBook')
bookmarkButton.forEach(function (setIt) {
  setIt.addEventListener('click', () => {
    setIt.classList.toggle('bookmarkChecked')
  })
})
.pictureBook {
  all: unset;
  background-image: url(/img/bookmark-svgrepo-com.svg);
  background-size: cover;
  width: 100px;
  height: 100px;
  position: absolute;
  top: -15px;
  right: -10px;
  cursor: pointer;
  opacity: 0.5;
}
.bookmarkChecked {
  opacity: 1;
}
  <section class="main__section">
    <h2 class="question__title">Question</h2>
    <p class="question__text">
      Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla eius
      voluptatibus modi voluptates nisi quam expedita fugiat repudiandae
      animi ab.
    </p>
    <button class="pictureBook"></button>
    <button class="showanswer__btn">Show Answer</button>
    <p class="answershowed hideanswer">
      Lorem ipsum dolor sit, amet consectetur adipisicing elit. A nemo
      libero tempore dolore numquam dolorum sed cumque nihil explicabo
      ullam.
    </p>
  </section>

By clicking the bookmark icon, the opacity changes to 100% indicating it has been marked (displayed in black).

I have implemented a button and would like it to only reveal the classes that have been bookmarked. I have reached a roadblock in my research and hope for assistance from this community.

Additionally, when another button in the footer is clicked, the site should display only the active sections with the class "bookmarkChecked" visible.

Your help would be greatly appreciated. Thank you!

Answer №1

To target only the bookmarked button, look for those with the bookmarkChecked class by using querySelectorAll.

  1. Loop through the list of buttons using forEach.
  2. In the callback function of forEach, find the corresponding section for each button using the closest method.
  3. Hide the bookmarked section by adding a class that sets the display property to none.
  4. If this sequence of actions should occur after clicking a button, add an addEventListener event listener.

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

Leveraging JavaScript's regular expressions to identify and capture the end of a

I am in need of using regular expressions (regex) to validate an ajax call to retrieve an output log from a server to check if a certain process is "finished". I have a PHP file that can provide the last line of the log under any circumstances. The Ajax ...

Visibility of the br tag is not detected

I need to keep one specific div visible on the page while hiding all other content. However, I am facing an issue where the <br> tag inside that div is not showing up. <div id='min320'>min 320px<br>screen width required</div ...

Instructions on how to automatically navigate to a different tab upon clicking an <a> element on a separate webpage, and subsequently display the designated tab on that

I have a button on my home page that, when clicked, should open a specific tab section on another page. The button is located on one page and the tabs are on a different page. On the second page, I have multiple tabs but will only mention one here, which ...

Page width incorrectly set leading to unnecessary scrolling

Having a bit of trouble with this layout - while everything looks good, the page extends to the right with just a blank space. I've tried everything but nothing seems to work. .container.open-sidebar { position: relative; left: 240px; } #sidebar ...

How to keep the bottom of a CSS {position:sticky; top:0} element within the viewport without using JavaScript

I am working on creating a dashboard frame using Bootstrap 5, and I aim to achieve the functionality shown in these two images: Here is my current setup: <html> <body> <div class="pretoolbar">pretoolbar (non-essential inf ...

Dealing with numerous https requests within a node.js application

I've been searching around on SO for assistance with this issue, but I seem to be going in circles without making any progress. My current project involves making multiple ReST POST requests using node.js https. I need to keep track of the responses ...

Guide to using VueJS for sending a POST request with a JSON array containing data and uploading a File or Picture for each element in the array

I am currently facing a challenge with Axios where I need to send formData from a dynamic Form to PHP. Users should be able to add multiple "persons" along with their respective data and pictures. Below is the format of the data: data: { person:{ ...

What could be causing the target to malfunction in this situation?

Initially, I create an index page with frames named after popular websites such as NASA, Google, YouTube, etc. Then, on the search page, <input id="main_category_lan1" value="test" /> <a href="javascript:void(0)" onmouseover=" window.open ...

When the ::after pseudo element is utilized for creating a bottom border, it will display following each individual list item contained within the specified div

I have been using a pseudo-element to create a division line between sections, and it was working perfectly until I added a list of items. Now, the division line is appearing after every <strong> and <li> tag. I have tried various approaches, s ...

Having trouble with the alignment of CSS menu in Safari?

Can someone please assist me with a strange issue I am experiencing on my website? The menu appears misaligned when using Safari, and it's been driving me crazy. I've tried troubleshooting for hours without success. Here is a link to the site: I ...

Navigating with Angular 1.5 Component router in conjunction with Express.js

I'm currently working on an Express application and I am trying to capture all routes to redirect users to /public/app/index.html: app.all('/*', function (req, res, next) { // Let's just serve the index.html for other files to enab ...

The condition for the result of a jQuery $.post call

I've customized this code obtained from the jQuery website by incorporating a condition into it. However, regardless of the outcome, it consistently enters the first 'if' statement. How can I ensure that my condition is functioning correctly ...

Building a Modal in React and Triggering it for Display, followed by Making an AJAX Request upon Submission

Check out my CodePen snippet (minus delete ajax request): http://codepen.io/martincarlin87/pen/KzPWOw I've been diving into the world of React recently, and I'm working on converting a page in my web application to utilize React instead of just ...

CustomJS TextBox callback to adjust range of x-axis: Bokeh

I'm currently working on a web page that features a plot powered by an AjaxDataSource object. However, I am facing a challenge with implementing a TextInput widget that can modify the xrange of this plot. Here is a snippet of my code: source = AjaxDa ...

Tips for utilizing an adaptive design with Angular

I am working on designing a page that allows for scrolling if needed. On each section of the page, I will be incorporating specific components with unique colors to enhance the layout. However, my current HTML code is not producing the desired result as sh ...

Is it possible to swap the src of the Next.Js Image Component dynamically and incorporate animations in the

Is there a way to change the src of an image in Nextjs by using state and add animations like fadein and fadeout? I currently have it set up to change the image source when hovering over it. const [logoSrc, setLogoSrc] = useState("/logo.png"); <Image ...

The response header does not contain a valid JWT token

Here's a function I've implemented to authenticate users by creating a response header: function verifyUser(res, id) { const payload = { id } const token = jwt.sign(payload, config.JWT_KEY, { expiresIn: '24h' ...

What is the best way to utilize d3.domain for extracting both d3.min and d3.max values from various columns within a JSON dataset?

I have a JSON file that contains data from different years for "male," "female," and "both" categories. I want to set the minimum value in my domain to be the lowest value across all three columns and do the same for the maximum value. Is there a way for ...

Construct a structure containing the key/value pairs of cells within an HTML table row using JavaScript

I'm completely new to JavaScript so please be patient with me; I'm not even sure if I'm searching for the solution correctly. Despite spending hours googling and experimenting, I still can't get it to work. My issue is related to an HT ...

Using the html5 file reader API in JavaScript to retrieve a file as a binary string and then sending it through an ajax request

I'm attempting to obtain the binary string of files, but I seem to be unable to do so. Why does readAsDataUrl work while readAsBinaryString doesn't? I have posted my code on jsbin and any help would be greatly appreciated. Thank you. Check out ...