How can two unique links toggle the visibility of divs with two specific IDs?

I am developing an interactive questionnaire that functions like a 'create your own adventure' story. The questions are shown or hidden depending on the user's responses. Below is the HTML code:

<!-- TIER 3
================================================== -->
<!--From Steak/Fries-->
<div id="SteakFries" class="hide">
    <p>I'd rather...</p>
    <span class="responseRead"><img class="prompt" src="http://fakeimg.pl/50" data-respnseto="Read">Read a Best-Seller on the Beach</span>
    <span class="responseExplore"><img class="prompt" src="http://fakeimg.pl/50" data-respnseto="Explore">Explore Ancient Ruins &amp; Natural Wonders</span>
</div>

<!--From Chamagne/Caviar
To travel style-->

<!--From Gastro-->
<div id="GastroPubs" class="hide">
    <p>I'd rather...</p>
    <span class="responseExplore"><img class="prompt" src="http://fakeimg.pl/50" data-respnseto="Explore">Explore Ancient Ruins &amp; Natural Wonders</span>
    <span class="responseVisitCity"><img class="prompt" src="http://fakeimg.pl/50" data-respnseto="VisitCity">Visit City Landmarks &amp; Theatres</span>
</div>

<!--From Wine/Cheese-->
<div id="GastroPubs" class="hide">
    <p>I'd rather...</p>
    <span class="responseVisitCity"><img class="prompt" src="http://fakeimg.pl/50" data-respnseto="VisitCity">Visit City Landmarks &amp; Theatres</span>
    <span class="responseRelaxParadise"><img class="prompt" src="http://fakeimg.pl/50" data-respnseto="RelaxParadise">Relax in an Over-Water Bungalo in Paradise</span>
</div>

<!--opens these divs-->
<!-- TIER 4 (Styles Discover)
================================================== -->
<!--From VisitCity-->
<div id="VisitCity" class="culturalChameleon hide">
    Cultural Chameleon
</div>

<!--From Champagne/VisitCity-->
<div id="RelaxParadise" class="fiveStar hide">
    Five-Star Fanatic
</div>

<!--From Explore-->
<div id="Explore" class="activeAdventurer hide">
    Active Adventurer
</div>

<!--From Read/VisitCity-->
<div id="Read VisitCity" class="sunSeeker hide">
    Sun Seeker
</div>
<!--================================================== -->
</div>

Based on the link clicked in Tier 3, the corresponding div in Tier 4 is displayed. This functionality is achieved with the following jQuery code:

var data = "";
$('.prompt').click(function(){
    data = $(this).attr('data-respnseto');
    console.log(data);
    $('.hide').hide();

    $('#' + data).fadeIn(600);
});

However, there is an issue with displaying the last item in Tier 4 when clicking either of the links in Tier 3 that have the attributes:

data-respnseto="Read"
data-respnseto="VisitCity"

Currently, the code only looks for one ID to display in Tier 4. Any suggestions on how to display item 4 in Tier 4 when the user clicks either of the links in Tier 3? Your help would be greatly appreciated.

Answer №1

Give this a shot:

let info = "";
$('.trigger').click(function(){
    info = $(this).data('details');
    console.log(info);
    $('.hide').hide();

    $("div[id*='"+ info + "']").fadeIn(600);
});

View JSFiddle Demo

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

Challenge with the Table Filtering feature in a React application

Having an issue with the filtering functionality in React and MUI. The problem arises when adding a row first and then attempting to filter - the filter stops working. However, if I filter immediately without adding a row, it works as expected. To reprodu ...

Using Vue.js to add animation effects to elements

What is the best way to use the .animate function on an element in vuejs? <aside v-transition v-if="toggleMenu"> <a href="#">Haha</a> <a href="#">Nice</a> <a href="#">Menu</a> </aside> A similar piece ...

Overlaying a div on top of an iframe

Struggling to make this work for a while now. It seems like a small issue related to CSS. The image isn't overlaying the IFrame at the top of the page as expected, it's going straight to the bottom. Here is the code snippet: .overlay{ width: ...

Is there a way to extract the "validade" value from the array and retrieve it exclusively?

The following array contains data: {"status":true,"data":[{"id":1,"pessoa_id":75505,"created_at":"2022-02- 01T17:42:46.000000Z","holder":"LEONARDO LIMA","validade&quo ...

I am facing an issue with my useFetch hook causing excessive re-renders

I'm currently working on abstracting my fetch function into a custom hook for my Expo React Native application. The goal is to enable the fetch function to handle POST requests. Initially, I attempted to utilize and modify the useHook() effect availab ...

Python: Utilizing the requests library to handle webpage redirections

Update: While this may not directly answer the question I had initially, I was able to find the necessary information in the API which provided a solution for my software. My current task involves logging into a webpage, navigating to another page, and ex ...

Issue in TypeScript where object properties may still be considered undefined even after verifying using Object.values() for undefined values

I'm encountering an issue with TypeScript regarding my interface MentionItem. Both the id and value properties are supposed to be strings, but TypeScript is flagging them as possibly string | undefined. Interestingly, manually checking that id and va ...

Connecting the search results page with the specific details page

Currently, I am developing a results page for my website and require assistance with linking each restaurant to a detail.php page. The project involves showcasing all the restaurants in San Francisco along with their health inspection scores, address, and ...

Transferring variables between vanilla JS and Angular 2: A guide

I am facing a challenge where I need to retrieve an object title from vanilla JavaScript and then access it in my Angular 2 component. Currently, I am storing the variable in localStorage, but I believe there must be a better approach. The issue arises wh ...

What is the reason for encountering an error when attempting to use a let variable in a new block before reassigning it

Check out this document here where I attempt to explain a coding scenario: // declare variables const x = 1; let y = 2; var z = 3; console.log(`Global scope - x, y, z: ${x}, ${y}, ${z}`); if (true) { console.log(`A new block scope - x, y, z: ${x}, $ ...

Angular constantly looping due to $watchCollection being triggered repeatedly

I am interested in monitoring the changes of an object, referred to as x, specifically its properties which are checkboxes. When a user checks a box, it alters the checked property (prop1) to 1. Each property contains certain content, and when enabled, it ...

Is there a way to send all the results of a Flask database query to a template in a way that jQuery can also access

I am currently exploring how to retrieve all data passed to a template from a jQuery function by accessing Flask's DB query. I have a database table with customer names and phone numbers, which I pass to the template using Flask's view method "db ...

Regular expression for precise numerical values of a specific magnitude (any programming language)

I have been searching for a solution to what I thought was a common problem but haven't found one yet. What I need is a regular expression that will fail on a specific number of significant figures (a Max), but pass for fewer than the Max. It should ...

Click to rotate the image - jQuery

I'm attempting to create a functionality where an image rotates upon clicking. Since this feature needs to work for multiple images, I understand the importance of utilizing the $(this) tag in jQuery. However, my initial attempt failed to produce the ...

Using sl-vue-tree with vue-cli3.1 on internet explorer 11

Hello, I am a Japanese individual and my proficiency in English is lacking, so please bear with me. Currently, I am using vue-cli3.1 and I am looking to incorporate the sl-vue-tree module into my project for compatibility with ie11. The documentation menti ...

Guide to Generating Extern for a Constant Variable in Google Closure Compiler (Variable Cannot be Renamed due to Eval)

Currently, I am using Google Closure Compiler in "SIMPLE_OPTIMIZATIONS" mode for my JavaScript code. However, I encountered an issue when the obfuscation changed the variable name from "_u" to "a", resulting in an error that "_u" is not defined in the cons ...

Fontawesome is unable to update the class due to the presence of invalid characters in the string

const toggleDarkOrLight = document.getElementsByTagName('i')[0]; var toggled = false; const toggleFunction = () => { if (toggled === false) { toggleDarkOrLight.classList.remove('fa fa-toggle-off'); toggleDarkOrLight.classLi ...

Processing a JSON array of objects in AngularJS

When using Angular's fromJson function to parse a JSON string, I encountered an issue. If the JSON is a simple array like "[1, 2]", the code works fine. However, I need to work with an array of dictionaries instead. var str = "[{'title' ...

Creating synchronous behavior using promises in Javascript

Currently, I am working with Ionic2/Typescript and facing an issue regarding synchronization of two Promises. I need both Promises to complete before proceeding further in a synchronous manner. To achieve this, I have placed the calls to these functions in ...

The issue of jQuery not functioning properly within a Rails environment

Despite my efforts, I am still struggling to make jquery work in rails. I have installed the 'jquery-rails' gem and added the require statements in the application.js file. Below is a sample test page that I have been testing: <!DOCTYPE htm ...