Only one div is revealed by Jquery show and hide, while the other remains hidden

I'm attempting to utilize a single href link to toggle the visibility of two other divs. The first div should be visible by default, while the second div remains hidden.

    <a href="#ben1" class="fa fa fa-times closer" > 
    <p> clickable text </p>
   </a>
    <a href="#ben2" class="fa fa fa-times closer" > 
    <p>  clickable text</p>
   </a>


<div class="resume right-col" id="ben1">
<p>Hello this is a test </p>
</div> 

<div class="resume right-col" id="ben2">
<p>Hello this is a test </p>
</div> 


<div class="resume right-col" id="ben1">
<p>second text area i need displayed by default</p>
</div> 

<div class="resume right-col" id="ben2">
<p>second text area i need hidden initially/p>
</div> 


<script>
$('.resume') .hide()
$('#ben1').show();
$('a[href^="#"]').on('click', function(event) {
$('.resume') .hide()
var target = $(this).attr('href');

$('.resume'+target).toggle();
e.preventDefault();
});
</script>

Upon initial loading of the page, the second div identified with "ben1" always remains hidden. Is there something missing from the script that I need to incorporate?

Answer №1

To improve the structure, consider creating a class for displaying content instead of relying on IDs. Here's an example:

    <a href="#ben1" class="fa fa fa-times closer" > 
    <p> link text </p>
   </a>
    <a href="#ben2" class="fa fa fa-times closer" > 
    <p> link text </p>
   </a>


<div class="resume right-col class1" id="ben1">
<p>Hello this is a test </p>
</div> 

<div class="resume right-col class2" id="ben2">
<p>Hello this is a test </p>
</div> 


<div class="resume right-col class1" id="ben1">
<p>second text area i need displayed by default</p>
</div> 

<div class="resume right-col class2" id="ben2">
<p>second text area i need hidden initially/p>
</div> 


<script>
$('.resume') .hide()
$('.class1').show();
$('a[href^="#"]').on('click', function(event) {
$('.resume') .hide()
var target = $(this).attr('href');

$('.resume'+target).toggle();
e.preventDefault();
});
</script>

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

Firefox fails to apply styling to content loaded through Ajax (particularly when using AngularJS)

I've encountered a strange problem while working on my app (MVC4 + AngularJS). It seems that in Firefox (currently version 42), content loaded through an Ajax call does not always pick up the CSS styles that were loaded before. In the attached images, ...

Troubleshooting: Magento checkout page keeps scrolling to the top

We are experiencing an issue where, during the one page checkout process, the next step is not automatically scrolling to the top of the page when it loads. After a user fills out all their billing information and clicks continue, the next step appears ha ...

use jquery to send form data and display response dynamically without refreshing

Jquery: <script type="text/javascript"> $(document).ready(function(){ $("form.register").change(function() { $.post("check.php", $("form.register").serialize(), function( data ) { if( data.username == "inuse" ) ...

Tips on enhancing the appearance of your numberbox with vibrant colors

Hello everyone! I am working with some devextreme code blocks and need help in changing the border color of a numberbox to red. Can anyone guide me on how to achieve this? import React, { useState } from 'react'; import { NumberBox } from 'd ...

Updating the color of text when clicking on a link using javascript

I am facing an issue where I cannot seem to change the text color using JavaScript. The functionality to show and hide div tags is working perfectly, but changing the color seems to be a challenge. It's worth noting that there are no styles defined fo ...

Employ jQuery to locate every image on a separate webpage using ajax in order to preload them

I am currently developing an app that has been converted from flash to html/js and is utilizing jQuery. Each page/scene is being pulled in via ajax, and I have a 'loading' overlay set up until these pages are fully loaded. However, I also want to ...

Insert HTML code at the top of a WordPress page

Currently, I am in search of a solution for the following issue: I want to include a black bar at the top of every page using a plugin (similar to the WordPress admin bar that appears when you are logged in at wp-admin). Initially, I considered adding th ...

Invoking a function in an Angular 4 component using a <td> element

Take a look at this block of code: personListComponent.html <tr *ngFor="let person of personService.getPersons()"> <td (onShow)="getCountry(person)">{{person.name}}</td> <td>{{country}} </tr personListComponent.ts ...

Is there a way to change the fill color of an SVG circle using Thymeleaf?

I am struggling to change the background color of a circle in an SVG image on my HTML page using Thymeleaf. I attempted to use inline style tags, but it resulted in the circle's background color turning black. <svg id="svg" height="50" width=" ...

"Utilizing Bootstrap CSS along with a variety of other CSS/HTML component bundles

Recently diving into the world of Bootstrap for my ASP.NET MVC project has been a game-changer! I'm in awe of its capabilities and functionality. However, I can't help but wish there were more ready-to-use components available to streamline the d ...

Display elements with a particular class using jQuery

I want to utilize jQuery to show all elements within a UL that have the class .active. Is this how my code should look? if ($(this).hasClass('active')) { $( ".active" ).show(); } This is my HTML structure <ul> <li>&l ...

The WordPress issue of a background image not displaying properly when using both a linear gradient and an image

Code snippet: style="background-image: linear-gradient(151deg,#6a7a8f 9%,#8692A4 30%,#a9b1be 49%, #ced2d9 66%, #f1f0f1 88%),url(<?php echo $page['image']; ?>);" Within the div, only the linear gradient is visible. When I switch the order ...

What makes the nav class different from the navbar class in Bootstrap?

Recently, I delved into learning about bootstrap and web development. To my surprise, I stumbled upon the nav and navbar classes in bootstrap 4. I'm curious to know what sets them apart from each other and when it's best to use one over the other ...

Utilize jQuery to extract information from a webpage and retrieve a JSON object

Looking to extract specific JSON data from a webpage on the same domain that is nested inside a <script> tag with a unique id. <div id="someID"> <script type="text/json" id="scriptID"> { "some more here": "and there", ...

Issue with event binding on datepicker that is dynamically added is not functional

I have integrated the Kendo datepicker into my project, and I am facing an issue with duplicated div elements containing datepickers. The problem is that the datepicker events only fire once on the first duplicated div and then stop working altogether. I ...

PHP - The variable $_SESSION['var1'] has not been set within the index.php file

My index.php page is causing me some confusion. <?php session_start(); if (!isset($_SESSION['var1'])) { echo "session not started..."; die(); } else { echo "session started"; die(); } ?> Within the page, there is a login form that connec ...

Ways to navigate to a different page while displaying an alert message?

Desperately seeking assistance with redirecting to another page and then triggering an alert("HELLO") once the new page is loaded. I have attempted the following approach: $.load(path, function() { alert.log("HELLO"); }); But using window.location o ...

Using AngularJS to execute jQuery code after the page has finished loading

I have a carousel of images on my website: Here is the code I am using: <div id="carousel"> <ul class="bjqs"> <li ng-repeat="image in dealer.images"><img src="{{image}}" alt="{{image}}" /></li> </ul& ...

Ways to adjust the number of columns in a div using Bootstrap for varying screen sizes of larger devices

I am new to coding and exploring html, css, and bootstrap. Is it possible to assign varying numbers of columns to a div based on different resolution ranges for larger devices? Specifically looking at the "lg" range which covers laptops and desktops with ...

Retrieve the value of each element within the elements array

I need help retrieving values from elements within an array. Specifically, I am looking to extract the value of the <span id="attendees"> element within each <a> element. For example: HTML: <a id="meetingItem_{{meeting.id}}" onclick="Auth ...