Toggle the visibility of div elements by clicking on another div

Hey everyone, I have this cool concept where clicking on different flags will reveal a corresponding list. However, my jQuery code doesn't seem to be functioning properly.

$('.canadaflag').click( function() {
    $(".canadalocations").toggleClass("viewlocationlist");
    $(".usalocations").toggleClass("hidelocationlist");
    $(".guatemalalocations").toggleClass("hidelocationlist");
    $(".colombialocations").toggleClass("hidelocationlist");
    $(".perulocations").toggleClass("hidelocationlist");
} );

$('.usaflag').click( function() {
    $(".usalocations").toggleClass("viewlocationlist");
    $(".canadalocations").toggleClass("hidelocationlist");
    $(".guatemalalocations").toggleClass("hidelocationlist");
    $(".colombialocations").toggleClass("hidelocationlist");
    $(".perulocations").toggleClass("hidelocationlist");
} );

$('.colombiaflag').click( function() {
    $(".colombialocations").toggleClass("viewlocationlist");
    $(".usalocations").toggleClass("hidelocationlist");
    $(".canadalocations").toggleClass("hidelocationlist");
    $(".guatemalalocations").toggleClass("hidelocationlist");
    $(".perulocations").toggleClass("hidelocationlist");
} );

$('.guatemalaflag').click( function() {
    $(".guatemalalocations").toggleClass("viewlocationlist");
    $(".usalocations").toggleClass("hidelocationlist");
    $(".canadalocations").toggleClass("hidelocationlist");
    $(".colombialocations").toggleClass("hidelocationlist");
    $(".perulocations").toggleClass("hidelocationlist");
} );

$('.peruflag').click( function() {
    $(".perulocations").toggleClass("viewlocationlist");
    $(".usalocations").toggleClass("hidelocationlist");
    $(".canadalocations").toggleClass("hidelocationlist");
    $(".colombialocations").toggleClass("hidelocationlist");
    $(".guatemalalocations").toggleClass("hidelocationlist");
} );

If anyone could take a look and assist me with this, here is the link to the fiddle:

https://jsfiddle.net/isaacflyingsquirrel/x74zq5mL/

Answer №1

Check out this interactive example.

If you want to implement this functionality, you can make use of HTML data attributes like shown below:

$('.flagwrapper div').click(function() {
  var flag = $(this).attr('class');

  $('.flag').hide();
  $('div[data-location="' + flag + '"]').show();
});
.canadaflag {
  background-color: orange;
  width: 20px;
  height: 20px;
}

.usaflag {
  background-color: red;
  width: 20px;
  height: 20px;
}

.colombiaflag {
  background-color: green;
  width: 20px;
  height: 20px;
}

.peruflag {
  background-color: yellow;
  width: 20px;
  height: 20px;
}

.guatemalaflag {
  background-color: purple;
  width: 20px;
  height: 20px;
}

.flag {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="flagwrapper">
  <div class="canadaflag"></div>
  <div class="usaflag"></div>
  <div class="colombiaflag"></div>
  <div class="peruflag"></div>
  <div class="guatemalaflag"></div>
</div>

<div class="flag canadalocations" data-location="canadaflag">
  <p class="plocation">North Calgary</p>
  <p class="plocation">South Calgary</p>
  <p class="plocation">Hamilton</p>
  <p class="plocation">London</p>
  <p class="plocation">Ottawa</p>
  <p class="plocation">Victoria</p>
  <p class="plocation">Winnipeg</p>
</div>

<div class="flag usalocations" data-location="usaflag">
  <p class="plocation">Missoula</p>
  <p class="plocation">Lutz</p>
  <p class="plocation">Bozeman</p>
  <p class="plocation">Kansas City</p>
</div>

<div class="flag guatemalalocations" data-location="guatemalaflag">
  <p class="plocation">Guatemala City</p>
</div>

<div class="flag colombialocations" data-location="colombiaflag">
  <p class="plocation">Medellin</p>
  <p class="plocation">Cali</p>
  <p class="plocation">Bucaramanga</p>
  <p class="plocation">Bogota</p>
  <p class="plocation">Baranquilla</p>
  <p class="plocation">Ibague</p>
</div>

<div class="flag perulocations" data-location="peruflag">
  <p class="plocation">Lima</p>
</div>

Answer №2

Seems like jquery wasn't imported, which is why it's not working. Click on the javascript tag, then go to frameworks & extensions, scroll down until you find jquery, and run it again. It should start working.
Also, avoid using toggle class; instead, use show()/hide() as shown below:

$('.peruflag').click( function() {
    $(".perulocations").show();
    $(".usalocations").hide();
    $(".canadalocations").hide();
    $(".colombialocations").hide();
    $(".guatemalalocations").hide();
} );

Replace .toggleClass("viewlocationlist"); with .show() and .toggleClass("hidelocationlist"); with .hide()

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

How can I incorporate my styles into a separate css file for use in a React project?

Although it may seem simple, I am interested in incorporating SCSS into my React application for styling purposes. I understand that React will interpret all of my styles and render them in separate <style> tags within the <head> section. I hav ...

Are there any web browsers that automatically switch to a non-SSL connection if an attempt to connect with SSL

I regularly utilize jQuery along with jQuery.ajax to make connections between pages. I am interested in establishing a connection from a non-SSL page to an SSL page using ajax. Are there any web browsers that will attempt to connect via non-SSL if the con ...

Getting HTML from Next.js middleware - a step-by-step guide

Is there a way to send the HTTP Status Code 410 (gone) together with a customized HTML message? I want to display the following content: <h1>Error 410</h1> <h2>Permanently deleted or Gone</h2> <p>This page is not foun ...

Triggering a class in Angular when another class is activated through JavaScript

My goal is to apply the class "xyz" when the class "xy" is activated using ngClass. I am looking to achieve the following scenario: If the class "xyz" is present in the tag, then activate the class "xy" Using ngClass="'xyz', 'xy'" ...

capture the alteration of text within an input field

Currently, I am utilizing the Joomla calendar feature to select a date. The HTML code for this functionality is displayed below. When you click on the image, a calendar will pop up allowing you to choose a date. Once selected, the popup closes and the date ...

Is there a way to showcase an HTML body in the Gmail app on iOS?

I am facing a challenge while trying to incorporate HTML formatting in the body of an email shared through Gmail using UIActivityViewController on iOS. Despite my efforts, the Gmail application fails to render the HTML content properly and only displays pl ...

Console does not display AJAX response

Currently, I am utilizing AJAX to fetch form data from a Django application and my objective is to display the response in the console. $.ajax({ type: 'GET' , url: url, data: {'PUITS': PUITS ...

Issue encountered: Inoperable binding when employing ko.mapping with two distinct models

I've been struggling to implement a drop-down select in conjunction with a table on a page using knockout bindings. The issue arises when I try to use the mapping options in the knockout binding plugin – either the drop-down or the table behaves inc ...

The browser prevented the script located at “http://127.0.0.1:5500/assets/platform.png” from loading due to an invalid MIME type of “image/png”

Sorry if this question seems repetitive, but despite extensive searching, I haven't been able to find a solution to my specific problem. I am currently working on developing a basic JavaScript game, but I'm facing challenges when it comes to impo ...

Having trouble getting your Bootstrap v4 carousel to function properly?

Currently, I have implemented the carousel feature from Bootstrap v4 in a Vue web application. I am following the same structure as shown in the sample example provided by Bootstrap, but unfortunately, it is not functioning correctly on my local setup and ...

Trouble with CSS loading due to div in Visual Studio MVC

Currently, I am working with Visual Studio 2013 MVC and have a situation regarding the styling of my navbar in _Layout. The navbar includes a messages dropdown menu that utilizes CSS and JS extensively. Interestingly, when I load the messages as a partial ...

Angular-UI keypress event allows users to interact with components and

I am facing challenges while using the keypress feature in the following code snippet- <div ng-controller="GBNController"> ... <input id="search-field" type="text" placeholder="JSON Query" ng-model="queryText" ui-keypress="{enter: foo()}"/> .. ...

"How to prevent users from using the back button on Google Chrome and Edge browsers

window.history.pushState(null, null, location.href); window.addEventListener('popstate', () => { history.go(1); alert('The use of back button is restricted.'); }); An issue has been identified where the code snippet above d ...

What is the best way to generate a linked list from a JSON array?

I have a list of universities that I generated from a JSON file and now I want to create hyperlinks for each university in the list so that users can navigate to their respective university pages. HTML <ul data-bind="foreach: university"> <li ...

Internet Explorer will automatically apply a blue border to a div element when a CSS gradient is utilized

I'm attempting to create a gradual fading gray line by using a div that is sized at 1x100px with a CSS gradient applied for the fade effect. The only issue I am encountering is in Internet Explorer where the div is displaying a blue border which I am ...

Activate scroll event when image src changes in Angular using jQuery

Seeking assistance with utilizing jQuery to scroll to a specific thumbnail inside a modal when left/right arrows are pressed. The modal should appear when the user clicks on an image. I've managed to implement scrolling upon clicking a thumbnail, but ...

Adjustable icon dimensions in Material-UI

My current setup involves utilizing material-UI icons <ArrowRightAlt/> I have been able to manipulate the size of the icon using the fontSize attribute <ArrowRightAlt fontSize="large" /> However, I am interested in having the size ...

Script tag causing browser to send unwanted request

Recently, I received some code from a third-party supplier to implement on certain web pages. This code utilizes the jQuery plugin for jTemplates and looks something like this: <script type="text/html" id="item_template"> {#foreach $T.search.results ...

What is the best way to create stylish corners on my website?

I'm struggling with creating a corner like the one shown in the image. As a beginner in frontend development, I attempted to write the following code but now I'm stuck. The Snippet: * { margin: 0; padding: 0; } body { height: 100vh; ...

Customize Selected Options in Multi SelectPicker in Real-Time

In my code, I am working with a selectpicker and using JavaScript to set the selected options dynamically. The syntax for this is: $('.mySelectPicker').selectpicker('val', ['one', 'two', 'three']); As par ...