Display a popup when a label is clicked, and update the popup contents according to the label chosen

Upon clicking any label in the #nav section, a popup is displayed. However, I want the content inside the popup to change depending on which label is clicked. Currently, the different popup content divs will all hide or show based on CSS only, so I believe my jQuery selector might be incorrect. How can I resolve this issue? Would it be better to add or remove active classes instead of using a radio setup? Thank you for your help!

UPDATE1: After fixing the typo mentioned in the comments, it seems that my current issue is not displaying the other unchecked-radio targets.

UPDATE2: By adding

$('[id^="c-"]').hide();
, I am able to display only one target div correctly, but it requires two clicks to work. (One click will display the previous state of the popup, such as empty if nothing was clicked yet, or #c-ask if its label was the last one clicked.)

HTML:

<input hidden type="radio" id="ask" name="popinfo"/>
<input hidden type="radio" id="sub" name="popinfo"/>
<input hidden type="radio" id="mnu" name="popinfo"/>
<div id="fade">
    <div id="popup">
        <div id="c-ask">ask</div>
        <div id="c-sub">sub</div>
        <div id="c-mnu">mnu</div>
    </div>
</div>
<section id="splash">
    <div class="info">
        // 
    </div>
    <div id="nav">
        <i class="fa fa-home"></i>
        <label for="ask"><i class="fa fa-envelope"></i></label>
        <i class="fa fa-user"></i>
        <label for="sub"><i class="fas fa-edit"></i></label>
        <label for="mnu"><i class="fas fa-plus"></i></label>
    </div>
</section>

CSS:

#splash {
    width:100%;
    height:100vh;
    background:#ff0;
    display:flex;
    flex-direction:column;
    justify-content:center;
    align-items:center;
    text-align: center;
}

.info {margin: auto;}

#nav {
    margin-bottom:0;
    display:flex;
    flex-direction:row;
    justify-content:center;
    align-items:center;
    justify-content:space-evenly;
    width:100%;
    height:40px;
    background:#f00;
}

#c-ask, #c-sub, #c-mnu {display:none;}

#fade {
    width:100%;
    height:100%;
    position:fixed;
    top:0; left:0;
    background:rgba(0,0,0,0.5);
    display:none;
    z-index:1;
}

#popup {
    display:none;
    z-index:2;
    width:50%;
    height:50%;
    background:#fff;
    border: 1px solid #000;
    margin:auto;
    padding: 20px;
    overflow-x:hidden;
    overflow-y:auto;
    position: absolute;
    top:50%; left:50%;
    transform: translate(-50%, -50%);
}

jQuery:

$('label').click(function(){ 
    $("#fade").fadeToggle();
    $("#popup").fadeToggle();           
    var $target = $('input[type="radio"]:checked').attr('id');
    $('[id^="c-"]').hide();
    $("#c-" + $target).show();
});
$("#fade").click(function(){
    $(this).fadeOut();
    $("#popup").fadeOut();
});

Answer №1

It's more efficient to utilize the :radio change event rather than the label click event

$('input:radio:hidden').on('change',function(){  // radio change event
    $("#fade , #popup").fadeIn();           
    var $target = $(this).attr('id'); // utilize $(this)
    $('[id^="c-"]').hide().filter("#c-" + $target).show();
});
$("#fade").click(function(){
    $("#fade , #popup").fadeOut();
    $('input:radio:hidden').prop('checked' , false); // uncheck all hidden radios when pop up is hidden
});

$("#popup").on('click' , function(e){
  e.stopPropagation();
});
#splash {
    width:100%;
    height:100vh;
    background:#ff0;
    display:flex;
    flex-direction:column;
    justify-content:center;
    align-items:center;
    text-align: center;
}

.info {margin: auto;}

#nav {
    margin-bottom:0;
    display:flex;
    flex-direction:row;
    justify-content:center;
    align-items:center;
    justify-content:space-evenly;
    width:100%;
    height:40px;
    background:#f00;
}

#c-ask, #c-sub, #c-mnu {display:none;}

#fade {
    width:100%;
    height:100%;
    position:fixed;
    top:0; left:0;
    background:rgba(0,0,0,0.5);
    display:none;
    z-index:1;
}

#popup {
    display:none;
    z-index:2;
    width:50%;
    height:50%;
    background:#fff;
    border: 1px solid #000;
    margin:auto;
    padding: 20px;
    overflow-x:hidden;
    overflow-y:auto;
    position: absolute;
    top:50%; left:50%;
    transform: translate(-50%, -50%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css" integrity="sha256-2XFplPlrFClt0bIdPgpz8H7ojnk10H69xRqd9+uTShA=" crossorigin="anonymous" />

<input hidden type="radio" id="ask" name="popinfo"/>
<input hidden type="radio" id="sub" name="popinfo"/>
<input hidden type="radio" id="mnu" name="popinfo"/>
<div id="fade">
    <div id="popup">
        <div id="c-ask">ask</div>
        <div id="c-sub">sub</div>
        <div id="c-mnu">mnu</div>
    </div>
</div>
<section id="splash">
    <div class="info">
        Info
    </div>
    <div id="nav">
        <i class="fa fa-home"></i>
        <label for="ask"><i class="fa fa-envelope"></i></label>
        <i class="fa fa-user"></i>
        <label for="sub"><i class="fas fa-edit"></i></label>
        <label for="mnu"><i class="fas fa-plus"></i></label>
    </div>
</section>

Note: I personally suggest using data attributes instead of id for referencing elements

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

Adding a border-top overlay to vertical navigation

I'm working on a vertical navigation bar and I'd like to make some styling changes. Here's the current setup (View jFiddle): <style> ul{ list-style-type: none; } li{ display: block; margin: 0; padding: 10; border-top: 1px d ...

Incorporating an SVG Element into a design while ensuring its responsive functionality

I tried adding an SVG wave type picture to my background and making it responsive, but encountered some issues. I wanted to enhance the look of my existing background image by incorporating a wave design. However, when I zoomed in on the website, the new a ...

Using conditional CSS in React/Next.js

While using Next.js, I have implemented conditional rendering on components successfully. However, I am facing an issue where the CSS styles differ between different components. Code 1: import React from "react"; import Profile from "../../ ...

The special function fails to execute within an "if" condition

As a newcomer to JavaScript/jQuery and Stack Overflow, I kindly ask for your patience in case there are any major errors in my approach. I am currently developing an HTML page with Bootstrap 3.3.7, featuring a pagination button group that toggles the visib ...

A guide on transferring radio button values to another program and saving them into a database with the help of PHP and jQuery

I have encountered an issue with storing radio button values in a database. Specifically, I want to assign the value of 1 for "Yes" and 0 for "No". To accomplish this, I am utilizing two scripts - a.php and b.php. The former obtains the radio button values ...

Having trouble parsing the JSON object values within the Error section of the Ajax call

When making an Ajax call to a rest web API, I encountered an error that I need help resolving. Here is the code snippet: $.ajax({ url: "/********/getbytitle('****')/items", type: "POST", contentType: "applicat ...

Is there an issue with JQM plugin due to jQuery's append() function?

I am just starting to learn about jQuery mobile as I develop an app using it. Whenever I click on the Add_details button, I want to add dynamic input fields only once. After saving, if I click on the Add_details button again, I need to append more dynamic ...

Adjusting the height property to zero in the absence of an element

I'm currently working on creating a timeline for changeLogs and populating it with data from an AngularJS controller. The issue I'm facing is that the height of the timeline "Line" is set to 6px. This means that even when there is no data in the ...

Determine the number of times a user has accessed a page on my website

Is there a way to track how many times a user loads pages on my website using JavaScript? Could it be done like this? var pageLoads = 1; $(document).ready(function(){ var pageLoads ++; }); Should I store this information in a cookie? ...

I want to utilize an Ajax/JQuery filled dropdown to dynamically alter the user interface without the need for additional server-side requests

I am working on a feature where an AJAX post method populates a dropdown with object descriptions. But, I also need to update a textbox with the selected ID when changing the selection, without making another AJAX call. Here's the relevant code snipp ...

Determining the number of words in every line within a textarea

I am looking to determine the number of words per line in a textarea. The width of the textarea is variable. Check out this code snippet that calculates the number of rows: http://jsfiddle.net/2tcygj9e/ ...

Tips for ensuring all draggable elements have been successfully dropped using jQuery's droppable feature

Currently, I am developing a website that incorporates a drag and drop functionality. In this system, there are 3 buttons that can be dragged and dropped into an array. The elements in the array are also droppable, allowing them to receive buttons from t ...

Creating a border around an SVG element using the background color of its parent container

I am aiming to layer 2 SVGs on top of each other. The top SVG will have an outline to make it stand out more from the bottom SVG. These SVGs are embedded on buttons with transitions and hover colors. I want the outline SVG to match the background color of ...

Remove the image from the container in Bootstrap

I'm looking to position an image outside the container so that the right margin remains within the default container width (responsive) while keeping the image aligned with a button consistently. Please refer to the image below for clarification: ht ...

When the draggable item is released near the drop zone, allow drag and drop to combine

In my latest project, I created a fun game that involves matching different shapes. The goal is to drag the correct figure next to its corresponding shadow figure for a perfect match. Currently, if you partially overlap the square with its shadow, the game ...

Struggling to construct a binary tree as my descendants are not arranged in the right sequence

I am currently working on building a binary tree using PHP, MySQL, and a jQuery plugin developed by Frank-Mich. Here is the progress I have made so far... DATABASE STRUCTURE CREATE TABLE IF NOT EXISTS `members` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...

The jQuery mouseout functionality seems to be malfunctioning and not responding as expected

I am currently developing a slider that displays details when the mouse hovers over the logo, and hides the details when the mouse moves away from the parent div. jQuery('.st_inner img').mouseover(function() { jQuery(this).parent().siblings( ...

Creating a Basic JQuery Dialog Box

Issue with jQuery Trigger <script type="text/javascript" src="simpledialog.js"></script> <link href="simpledialog.css" type="text/css" rel="stylesheet" /> <script type="text/javascript"> $(document).ready(function () { $('.s ...

Animation of lava effect in Angular 7

I have a unique Angular 7 application featuring a homepage with a prominent colored block at the top, along with a header and various images. My goal is to incorporate lava effect animations into the background similar to this CodePen example. If the link ...

methods for performing multiplication on dynamically inserted variables using jquery

I'm trying to dynamically calculate the total amount based on the price and product quantity after changing the value in the #quantity text-box. I implemented the following solution, but upon checking the Firefox console, I noticed that there is no ca ...