Step-by-step guide on replacing a visible div with a new div when a button is clicked

I have organized my website with links on the left sidebar and matching content in the right sidebar, structured within appropriate div elements.

My goal is to display only one div at a time when a user clicks on one of the links located on the left side. Can someone provide assistance with this functionality?

The HTML structure is as follows:

<div id="sidebar2">
       <div id="companyimage"><img src="siteimages/imagetest.jpg"></div>
       <div id="historyimage"><img src="siteimages/imagetest2.jpg"></div>
       <div id="awardsimage"><img src="siteimages/imagetest3.jpg"></div>
       <div id="publications"><img src="siteimages/imagetest4.jpg"></div>
       <div id="portfolio"> Embedded an Issuu PDF here. </div>
 </div>

The links can be found in sidebar 1.

I utilized jQuery and successfully implemented code to hide one div and show another. However, I am unsure how to handle scenarios where users click on random links. Ideally, I want to hide the current div and reveal the corresponding one.

Here is the script I currently have:

<script>
   $(document).ready(function(){
   $("#showpublications").click(function(){
   $("#companyimage").hide();
   $("#publications").fadeIn(1500);
   });
 });
</script>  

Any form of assistance on this matter would be greatly appreciated!

Answer №1

Here is a simple solution I put together for you. It may require some adjustments, but it should help you get started.

Check out the code here

jQuery:

 $("#sidebar2 div").hide();

 $("#sidebar1 a").click(function(){
     //Hide all DIVs first
     $("#sidebar2 div").hide();

     //Then show the selected one.
     var divToShow = $(this).attr("href");
     $(divToShow).show();
 });

HTML:

<div id="sidebar1">
<a href="#companyimage">companyimage</a>
<a href="#historyimage">historyimage</a>
<a href="#awardsimage">awardsimage</a>
<a href="#publications">publications</a>
<a href="#portfolio">portfolio</a>
</div>
<div id="sidebar2">
   <div id="companyimage">companyimage</div>
   <div id="historyimage">historyimage</div>
   <div id="awardsimage">awardsimage</div>
   <div id="publications">publications</div>
   <div id="portfolio"> Embedded an Issu PDF here . </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

What could be causing AJAX to consistently return identical data to the callback function when distinct objects are supplied as arguments?

I am attempting to make two separate calls to an MVC controller. The first call returns a series of data, while the second call returns a partial view. When I check in Firebug, both calls show "success" status. I am trying to utilize a callback function t ...

Modify certain user configurations without altering all settings in one specific form using PHP and MySQL

I am in the process of developing a PHP website with data stored in a MySQL database. Currently, I have completed the sign-up, login, and logout functionalities. However, I am looking to enhance user experience by adding a section where users can adjust th ...

How to automatically set a default bootstrap tab upon page load using an MVC JsonResult responseData

I'm looking to enhance the user experience on my dashboard by allowing users to set their preferred default tab view upon logging in. Using an AJAX call to retrieve the value from the database in MVC, I am working on the JS/Razor side of things to mak ...

Display a popup notification when clicking in Angular 2

Can anyone help me with displaying a popup message when I click on the select button that says "you have selected this event"? I am using Angular 2. <button type="button" class="button event-buttons" [disabled]="!owned" style=""(click)="eventSet()"&g ...

Struggling with properly navigating a JSON file in my JavaScript code... specifically trying to access and retrieve the "responseObject.weather[i].weatherresponseObject.weather[i].description" data

Struggling with extracting data from a PHP file in JSON format to display on an HTML file using JavaScript. I seem to be encountering issues with accessing responseObject.weather[i].weatherresponseObject.weather[i].description, and I suspect it might be d ...

WCF API encountering issue with Ajax request

I encountered the following error: The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebC ...

Ways to increase the date by one month in this scenario

I am facing an issue with manipulating two date variables. One of the dates is set to last Friday by default, and I am attempting to add a month to this date using the code snippet below. However, it does not produce the desired result. var StartDate = ne ...

The background remains hidden from view as a result of its positioning

There seems to be an issue with the background color of my list not displaying correctly. The problem arose after floating elements left and right. Any suggestions on how to resolve this? If you'd like to take a look, here's the link to the fidd ...

What is the best way to retrieve the title element from a loaded iframe?

Looking for a way to create a dynamic title for my iframe without manually coding it in. My goal is to extract the src of the iframe, load that object, and then retrieve the title element from it. I attempted using iframe.contentDocument but encountered ...

What is the best way to open the index.html file in electron?

I'm currently developing a cross-platform app using electron and Angular. As of now, the code I have for loading my index.html file looks like this: exports.window = function window() { this.createWindow = (theBrowserWindow) => { // Create ...

Tips for incorporating border/outline/stroke into SVG elements using CSS

Currently, I am incorporating SVG elements into a webpage using D3js. However, I am facing challenges when it comes to styling these elements as typical CSS syntaxes like path { border: 3px solid green; } do not seem to work. Is there a way to apply bo ...

What strategies can I use to include multiple spans within div elements without causing the code to become overly lengthy?

I'm working with 20 spans representing movie genres, each accompanied by another span that "closes" the genre when selected. The closing span has an .active class, similar to this example: My version currently looks like this: To achieve this, I hav ...

Aligning form fields in a HTML form using Bootstrap

In my extensive form, several fields within the setup encounter the same issue. Specifically, a checkbox below 'contact telephone number' causes the surrounding content to become misaligned. https://i.sstatic.net/8qI8Z.png My attempts to resolv ...

What methods are available for generating a short-lived banner using JavaScript?

I recently entered this industry and am in the process of constructing a website. Utilizing HTML and CSS, I crafted a cookie notification banner. I am seeking guidance on how to ensure that it only appears once "every 24 hours for each user." I attempted ...

Make an angularJS PUT request using $http

After selecting a value from the dropdown selection, I am attempting to make a PUT call in AngularJS. Although I have successfully retrieved the value from the selection, I am encountering difficulty in generating the response for the PUT call. Below is t ...

Looking to convert a jQuery function to plain JavaScript code?

Struggling with my homework, I must apologize for any mistakes in my English. My task involves creating a chat using node.js and I found some code snippets on a website "" which I used successfully. The issue now is that the chat relies on old jQuery libr ...

Tips for locating the previous CSS value before it was altered by javascript code

I am working on adjusting the CSS variables provided by the system using JavaScript with the following code: document.body.style.setProperty("--rh__primary-base", "rgba(254,80,0)"); However, when I inspect the element, I can see that t ...

Steps for executing a function when a directory is clicked

Currently, I am working on creating a file browser and facing some issues with calling a function when the user clicks on a directory. Initially, I tried using onClick inside an href in the class part (right before FUNCTION folderSize), intending to open ...

I experienced issues with the brackets software freezing up while using the Bootstrap min CSS file

Recently, I've been using brackets as my text editor and have run into an issue with the bootstrap.min CSS file. For some reason, it keeps freezing whenever I try to load it in brackets. Oddly enough, HTML files work perfectly fine, but ...

POST data not being sent via AJAX

I am trying to use AJAX to submit form data to a PHP function, but it seems like the data is not being transmitted. Here is the code for the HTML Form: <form onSubmit="return registration();" id="registration_form"> <input type="email" id="e ...