Automatically omitting a selector that mimics a switch

After conducting thorough research, I discovered a variety of solutions using jQuery and Vanilla. While one answer perfectly addressed my issue, it failed to integrate effectively with the rest of my code. So, I decided to modify the code from this Stack Overflow post Show / Hide Div on Click with JavaScript which worked well. However, there's a small catch... when I select an option, it displays as intended. But what I really need is for only one option to be visible at a time – meaning that selecting a new option should hide the previously selected one. Here's the adapted code snippet:

function setCup(elementId) {
    const element = document.getElementById('pymntCup');

    if (element.style.display === "block") {
        element.style.display = "none";
    } else {
        element.style.display = "block";
    }
    }
    function setUsd(elementId) {
    const element = document.getElementById('pymntUsd');

    if (element.style.display === "block") {
        element.style.display = "none";
    } else {
        element.style.display = "block";
    }
    }
.flag-icon-wrapper {
    margin-right: 20px;
    padding-top: 5px;
    }
    .flex {
    display: flex;justify-content:space-evenly;
    }
    h3 {
    text-align:center;
    }
    .is-shown-initially {
    display: block;
    }
    .is-hidden-initially {
    display: none;
    }
<h3>Select a payment mode</h3>
    <div class="flex">
    <span class="flag-icon-wrapper flex" onClick='setCup()'>
    <svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" x="0" y="0" width="50" height="22">
    <path d="m 40,3 v 7 h 3.3 q 3.4,0 3.4,-3 0,-3 -3.4,-4 z m 3.2,-3 q 3.3,0 5.1,2 1.7,2 1.7,5 0,3 -1.7,4 -1.7,2 -5,2 H 40 v 8 H 36.6 V 0 Z M 17.9,0 h 3.4 v 13 q 0,3 1,4 0.9,2 3,2 2,0 3,-1 1,-2 1,-5 V 0 h 3.3 v 13 q 0,5 -1.9,7 -1.8,2 -5.3,2 -3.6,0 -5.5,-2 -2,-2 -2,-7 z M 15.4,2 13.6,5 13.3,4 Q 11.4,3 9...

Answer №1

You have the option to set the other one as none

function hideElement(elementId) {
const element = document.getElementById('elementToHide');

//You can choose to hide the other element
document.getElementById('otherElement').style.display = "none";

if (element.style.display === "block") {
    element.style.display = "none";
} else {
    element.style.display = "block";
}

}

Similarly, you can do the same here too

function toggleElementVisibility(elementId) {
const element = document.getElementById('elementToShow');

//Here you can make the other one invisible
document.getElementById('anotherElement').style.display = "none";

if (element.style.display === "block") {
    element.style.display = "none";
} else {
    element.style.display = "block";
}

}

Answer №2

You've encountered some logic errors in the code that is responsible for hiding items. Here's the corrected version:

let cupElement, usdElement;

window.onload = () => {
  cupElement = document.getElementById('pymntCup');
  usdElement = document.getElementById('pymntUsd');
}

function setCup() {
  cupElement.style.display = cupElement.style.display === "block" ? "none" : "block";
  usdElement.style.display = "none";
}

function setUsd() {
  cupElement.style.display = "none";
  usdElement.style.display = usdElement.style.display === "block" ? "none" : "block";
}
.flag-icon-wrapper {
    margin-right: 20px;
    padding-top: 5px;
    }
    .flex {
    display: flex;justify-content:space-evenly;
    }
    h3 {
    text-align:center;
    }
    .is-shown-initially {
    display: block;
    }
    .is-hidden-initially {
    display: none;
    }
<h3>Select a payment mode</h3>
    <div class="flex">
    <span class="flag-icon-wrapper flex" onClick='setCup()'>
    <svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" x="0" y="0" width="50" height="22">
    <path d="m 40,3 v 7 h 3.3 q 3.4,0 3.4,-3 0,-3 -3.4,-4 z m 3.2,-3 q 3.3,0 5.1,2 1.7,2 1.7,5 0,3 -1.7,4 -1.7,2 -5,2 H 40 v 8 H 36.6 V 0 Z M 17.9,0 h 3.4 v 13 q 0,3 1,4 0.9,2 3,2 2,0 3,-1 1,-2 1,-5 V 0 h 3.3 v 13 q 0,5 -1.9,7 -1.8,2 -5.3,2 -3.6,0 -5.5,-2 -2,-2 -2,-7 z M 15.4,2 13.6,5 13.3,4 Q 11.4,3 9.3,3 6.5,3 4.9,5 3.4,7 3.4,11 q 0,8 5.9,8 2,0 3.8,-2 h 0.3 l 2,2 -0.5,1 Q 12.4,22 9.1,22 4.9,22 2.5,19 0,16 0,11 0,6 2.5,3 4.9,0 9.1,0 q 3.1,0 5.8,2 z" style="fill:#007aff;"/></svg>
    </span>
    <span class="flag-icon-wrapper flex" onClick='setUsd()'>
    <svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" x="0" y="0" width="50" height="22">
    <path  d="m 37.2,3 v 15 h 2.5 q 3.7,0 5.3,-1 1.6,-2 1.6,-6 Q 46.6,7 45,5 43.4,3 39.7,3 Z m 2.2,-3 q 5.4,0 8,3 2.6,2 2.6,8 0,5 -2.6,8 -2.6,3 -8,3 H 33.8 V 0 Z M 24.6,3 q -1.4,0 -2.1,1 -0.8,1 -0.8,2 0,1 0.4,2 0.5,0 1.8,1 h 1.6 q 2.7,1 3.9,2 1.2,2 1.2,5 0,3 -1.9,4 -1.9,2 -5.2,2 -1.3,0 -2.8,-1 -1.5,0 -2.7,-1 l 2.3,-3 q 1.5,2 3.3,2 1.7,0 2.6,-1 1,-1 1,-2 0,-2 -0.5,-2 -0.6,-1 -1.9,-1 L 23.2,12 Q 18.3,11 18.3,6 18.3,4 20,2 21.8,0 24.6,0 v 0 q 3,0 5.2,3 L 27.6,5 Q 27.4,5 27.2,5 27,4 26.1,4 25.4,3 24.6,3 Z M 0,0 h 3.4 v 13 q 0,3 0.9,5 0.9,1 3.2,1 2,0 3,-1 1.1,-2 1.1,-5 V 0 h 3.3 v 14 q 0,4 -1.9,6 -1.9,2 -5.5,2 Q 3.9,22 1.9,20 0,18 0,14 Z" style="fill:#007aff;"/></svg>
    </span>
    </div>
    <div class="flex">
    <div id="pymntCup" class="is-hidden-initially">
    <ul>
        <li>Payments from Cuba</li>
    </ul>
    </div>
    <div id="pymntUsd" class="is-hidden-initially">
    <ul>
        <li>Payments from around the World</li>
    </ul>
    </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

Encountering an issue with the v-carousel component from Vuetify: receiving a 'Cannot read property 't' of undefined' error message

Currently, I am trying to create an image carousel using Laravel along with Vue/Vuetify. The issue lies in the fact that the Vuetify v-carousel and v-carousel-item components are not rendering properly due to the error mentioned in the title. I have alrea ...

What is the best way to extract both the link and text from an <a> tag that contains another element in XPath?

In a similar way to my current data (which cannot be shared due to company policy), the example below is based on information extracted from this response and this response. The aim is to extract both the text of the <a> element and the link itself. ...

Guide to creating a radio button with the appearance of a toggle button

Is there a way to style radio buttons like toggle buttons using only CSS and HTML? I want the group of radio buttons to have the appearance of toggle buttons, while still maintaining their functionality as radio buttons. UPDATE: I am open to simply makin ...

Querying a Mongoose nested schema

I've created the following schema: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const ProjectSchema = require('./project.js') const ClientManagerSchema = new Schema({ name : { type : String, required ...

Attempting to insert the symbol "$gt" into a query for a search. {[CastError: Unable to convert value "[object Object]" to date at path "createdAt"]}

In the following code snippet: Reviews.find({createdAt : {"$lt" : app.locals.lastDate}}), I am trying to dynamically change the $lt to $gt. app.post("/scroll", function(req, res){ console.log("req.body...", req.body); var sortCreate = req.body.old ...

Adding a luminous glow effect to the <a> tag upon selection

Currently, I am working on integrating a navigation bar into a test site. I have noticed that some navigation bars highlight the current selected page by adding a bar underneath or creating a glowing effect around the <a> tag. I am unsure of the corr ...

Retrieve all colors from the style attribute

I'm on the hunt for a method to extract all CSS colors from a website. Currently, I have been able to manage internal and external stylesheets by utilizing document.styleSheets. However, my issue lies in the fact that any styles directly assigned to a ...

Tag editor assessing the input's width for SO

I've been working on a tag editor similar to Stack Overflow's and it's coming along nicely. The only issue I'm facing is calculating the width of the textbox after a tag has been selected by the user. For each tag added, I try to adjus ...

Tips for creating a new line in PHP when the value includes a comma

I am struggling to format information that includes commas into new lines. The current output I have is shown in the current display. I would like to display it with each comma creating a new line automatically. Below is my current code: <tr> <td ...

How can I resolve the Vue warning about an unknown custom element <password-input>?

I've been working on resolving an error within a component, but unfortunately, I'm still encountering issues. The error message is as follows: [Vue warn]: Unknown custom element: - have you registered the component correctly? For recursive co ...

Can a Typescript class type be defined without explicitly creating a JavaScript class?

I am exploring the idea of creating a specific class type for classes that possess certain properties. For example: class Cat { name = 'cat'; } class Dog { name = 'dog'; } type Animal = ???; function foo(AnimalClass: Animal) { ...

Executing a JavaScript function with jQuery

function launch() { $("p").text("Hey there", greet()); } function greet() { alert("Greetings! You have triggered another function"); } HTML: <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button onclic ...

Swapping Out Models in Three.js: A Guide to Replacing Object3D Models

I'm attempting to swap out an object3D for a character model, while retaining all of its attributes like the position. var object = new THREE.Object3D( ); object = models.character[0].clone( ); object.position.set( 100, 230, 15 ); scene.add( object.sc ...

What are the steps to changing the navbar's color when scrolling down?

I am having trouble changing the color of my navigation bar from transparent to black after the user scrolls down. Despite trying various methods and watching tutorials, I can't seem to get it to work. HTML <nav role='navigation' class= ...

Issue with the alignment of DIV container due to spacing issues

In the screenshot below, there is a DIV container with padding of 15px on the left and right. However, the contents are appearing spaced out downwards for some reason... Other than the left & right padding, no other styling seems to be affecting it. Adjus ...

Is it a breach of separation of concerns to validate using ng-pattern?

I have a requirement in Singapore to validate contact numbers entered by users. The number must start with 6, 8, or 9 and should have a total of 8 digits. I am currently utilizing ng-pattern on an input field with a regex solution, but I am concerned abo ...

Utilize jQuery to target elements containing more than one class

Suppose an element has several classes as shown below: class="btn btn-primary add-movie-button is-on" Is it possible to select this element using only one class name with jQuery, for example: $(".add-movie-button") Can the method .hasClass("is-on") be ...

Node.js data transmission: Sending information from server to client

In my node project, PUG/Jade files are used to render webpages. Every minute, a JS file updates a redis database and I want the GUI to reflect these changes without requiring a page refresh. Here is an overview of how the data is currently passed: User ...

Subsequent modal forms do not trigger Ajax responses

I have a list of items displayed on a page, with each item having a link that opens a unique modal. Inside this modal, there is a text field for entering a username and an AJAX call to display usernames when "@" is typed (pretty cool, right?). The issue I& ...

Choose from the options provided to display the table on the screen

One of the challenges I am facing involves a table with two columns and a select option dropdown box. Each row in the table is associated with a color - green indicates good, red indicates bad, and so on. My goal is to have all values displayed when the pa ...