Hide all subsequent elements until a designated tag is reached

I have a FAQ with messed-up formatting that looks like this:

<p><strong>How can I come see the animals perform?</strong><br>
Schedules and information about attending a performance can be found here:</p>
<p><a href="http://jupmingdolphins.com">Performance tickets</a></p>
<p>If no performances are listed, it means that none are scheduled in the
near future. The animals take a break between November and May.</p>

<p><strong>What's the answer to this question?</strong><br>
It's 42, of course.</p>

<h2>Header for More Questions</h2>

<p><strong>Is it true the dolphins have smartphones?</strong><br>
Yes, they use Android phones and text each other constantly.</p>
<p><b>Just kidding!</b> They are all Apple fan-fish and prefer iPhones.</p>

(etc)

And I'm trying to figure out:

  1. Some CSS (and probably jQuery) to hide everything except the questions, on page load.

  2. Simple jQuery where, when the user clicks on a <strong>-wrapped question, the answer slides down and appears below it. The issue is that the markup is not structured properly due to the CMS, with various elements separating each question. The answers do not have their own containers and there are also H2 subheads throughout which should remain untouched.

To handle the click action, I need code similar to:

$('strong').click(function() {
   // hide or reveal all elements from $(this) down,
   // and stop when we hit next <strong> or <h2>
});

Answer №1

Updated

The issue at hand is further complicated by the lack of individual tags for each question. This creates a challenge as you attempt to hide the parent tag while displaying the question itself, creating a nearly impossible task.

In order to work around this issue, I have devised a solution here. The workaround involves duplicating the questions and placing them within their own tag. However, it would be beneficial to encourage the creators of this markup to restructure it for better functionality.


To conceal non-question elements, you may consider using the following code:

$("#container").children().not($("strong").parent()).not("p").hide();

If you need to display an answer, you can utilize nextUntil like so:

$(this).parent().nextUntil($("strong").parent()).show();

View FIDDLE

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

Unique Soundcloud visualization using webglonDeletegist

After going through the SoundCloud documentation, I have been trying to figure out how to create a visualizer for SoundCloud tracks. The challenge is that in order to achieve this effectively, I need access to the source waveform. I suspect that direct acc ...

Traversing an object containing an array property using JavaScript loops

I am currently experimenting with a basic BlackJack game using JavaScript. Below are some snippets of my code: function card(suit, face) { this.suit = suit; this.face = face; switch (face) { case "A": this.faceValue = 11; ...

The button in the flex container is not wide enough due to center alignment

For my project, I implemented a CSS grid layout consisting of 20 rows and 20 columns (5% of screen for each cell). One particular page includes a button that initially fit within grid columns 5-8 and rows 6-9 without any issues. However, I wanted to center ...

How can I access the binary data source of an image using ASP.NET?

Is there a way to preview an image before uploading it in an ASP.NET webform? I have tried using the code below, but now I am stuck on how to upload the image to the server after clicking the Save button. When checking my codebehind, I found that the <i ...

There was an error that said: "Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type

I keep encountering the error Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type whenever I utilize the .apply() method but I'm unable to pinpoint the cause. You can review my code here. Upon loading jsfiddle, try clicking ne ...

I am experiencing difficulties with my jQuery animation function as it does not seem to be functioning properly despite successful

Can someone knowledgeable in jQuery and JavaScript please help me understand why this code isn't functioning as expected? The goal is for the image with the id "previmage" to expand and then shrink when the changeclass function is triggered. However ...

Exploring the iteration of JSON objects within an array using AngularJS

My auto search module has the following JSON structure. I need to iterate through an array of JSON objects and use keys and values as required. I have attempted the code below. However, with the provided JSON object, I am able to retrieve the key but not ...

Extract Initial Heading from RSS Using jQuery

Is there a way to only parse the first title from a list instead of parsing all titles? Any suggestions on how to achieve this? function parseFirstTitle(url, container) { $.ajax({ url: document.location.protocol + '//ajax.googleapis.com/aj ...

Welcome to the launch of OpenWeatherMap!

I just signed up for an account on the OpenWeatherMap website. My goal is to retrieve the current weather information for a specific location using the City ID API Call: http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=myAPIKey How ca ...

Leveraging findOne and findOneAndUpdate with an HTTP request in mongoose

I am currently developing an API Rest where I need to make HTTP requests using Postman. Specifically, I am trying to search for or update a MongoDB document by an ID that is not the default doc_id provided by Mongo. Models Schema 'use strict' ...

Trouble arises when attempting to retrieve the ID of the parent <ul> element using .parents() and .closest() in jQuery

Currently, I have 2 distinct <ul> containers with separate id's. Each of these contains a list of <li> elements. The first container is identified as <ul id="coaches-list">, while the second is labeled as <ul id="players-list"> ...

A layout featuring nested buttons and links within a card element, utilizing the power of Link in NextJs

After extensive searching on S.O., I have been unable to find a solution that works flawlessly. The issue at hand involves a card component in a NextJs application that is encompassed within a <Link> tag. Additionally, there is another <Link> t ...

Having trouble showing the text on the screen, but after checking my console, I notice empty divs with p tags. Surprisingly, the app is still functioning properly without any

Currently, I am developing a joke app entirely on my own without any tutorials. One of the components in the app is SportsJokesApi, which retrieves data from a local json folder (SportsJokesData) that I have created. Here is how it is structured: const Sp ...

What is the process for integrating ng-bootstrap into an Angular 5 project?

Has anyone encountered issues loading ng-bootstrap in their Angular project? I'm experiencing difficulties and would appreciate any insights. Thank you for your help! This is my app.module.ts file: import { BrowserModule } from '@angular/platf ...

Steps to obtain an Element binding from a Svelte component

Is it possible to retrieve the bounding client rect of an anchor element? I typically mark this using the syntax bind:this={someVariable}. However, when I add this code to a Svelte component, I receive a different object that Svelte uses to represent a c ...

Methods to keep react-table header and checkbox in place?

Currently, I am utilizing react-table with the selectTable HOC feature. My goal is to have both the checkbox and header fixed in place on my table. While I have been successful in individually fixing each component, I have encountered difficulties when att ...

Angular: The application has encountered an error due to an undefined property reading issue, resulting in a type error

Here is the code for the company.ts file: company.ts file - List item export class Company{ gstNo:number; contactNumber:number; contactName:string; phoneNo:number; address:string; companyName:string; } import { Injectable } ...

Include specific javascript files only for smartphones

Recently, I encountered an issue with a JavaScript code that swaps background images on scroll down. To address the problem with debounce, I ended up setting different debounce times for various browsers (I am aware this is not the ideal solution, but it&a ...

Discover the secret to smoothly scrolling to an element in ReactJs

Currently, I am in the process of constructing a Single Page Application (SPA) using React and one key functionality I need to implement is navigation that scrolls to specific sections on the page when clicked. This behavior is similar to how anchor tags w ...

When a new VueJS project is created, it failed to automatically install the necessary basic HTML files and folders

Hey there, I am completely new to Vue.js. Just recently, I installed the Vue.js/CLI and created a brand new project using vue create test. This prompted me to choose from three options: > Default ([Vue 2] babel, eslint) Default (Vue 3 Preview) ([Vue 3 ...