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

Error: operand a contains an invalid 'in' statement

After sending a request to the server using jQuery's $.ajax, I am getting back JSON data. $.ajax({ url: 'moreMonth.ajax', data: { startIndex: id }, success: function(data) { $.each(data, function(k, v) { alert(k + ':&ap ...

Displaying an image on a jsp page

In my current JSP, within the HTML section, I have the following: <select> <%if(size == 1)%> <option>None selected</option> <%if(size > 1)%> <option>1</option> </select> Additionally, I have this ima ...

Margin challenge in CSS

Just starting out with CSS, so please be patient with me. I'm currently working on customizing a form and everything is looking good, except for the confirmation label which is located in a div. I've managed to create space between other elements ...

Unexpected behavior encountered with jQuery code in HTML

I am attempting to add new li elements with img element inside a ul based on an array. However, only the first item in the array is being inserted. $.getJSON('/test-url/123').done (data) -> $.each(data, (index, value) -> $('.col ...

Updating a webpage using Ajax in Django

I've been diving into Django and am eager to implement ajax functionality. I've looked up some examples, but seem to be facing a problem. I want to display all posts by clicking on a link with the site's name. Here's my view: def archi ...

"Created a persistent fullscreen overlay over the body content, complete with scroll bars

Can a fixed fullscreen division position:fixed; width:100%; height:100%; cover the entire body of a page, including scroll bars? I understand that setting the body to overflow:hidden; can achieve this, but I am facing an issue where I want the fullscreen ...

Passing an array from PHP to JavaScript using AJAX

Here is the code snippet on the server side: <?php header('Content-Type: text/html; charset=utf-8'); require "../general.variables.php"; require "../functions_validation.php"; require "../functions_general.php"; require "../../db_con.php"; $ ...

What is the most efficient approach for handling numerous transactions individually through AJAX calls?

Just a heads up: there's quite a bit of pseudo code coming up, but it's necessary to illustrate my issue. My solution involves more complex management of ajax icons and statuses, but I've simplified it for clarity. Essentially, I'm fac ...

How to Attach a Click Event to LI or SPAN Elements Using JQuery?

I am having trouble getting a click event to work on an LI element within my webpage. Here is a snippet of the markup: <div id="Navigation"> <ul> <li class="navPrevNext inactive">|&lt; First Page</li> <li class="navP ...

Refreshing content with Ajax when the back button is clicked

Hey everyone, I've been working on an ajax site and I'm having trouble getting the content to reload when the back button is clicked. I'm using Dynamic Drives ajax content script along with a script that changes the URL onclick and a popstat ...

Creating CSS-style overlayed images that are resizable

I have a collection of images all the same size, and I want them to be overlaid on top of each other. I've tried setting the position of the images to absolute, but this causes an issue with the container not adjusting its size accordingly. Additiona ...

As you scroll, a box blocks off half of the screen

Hey everyone, I could really use your assistance! I'm working on developing a javascript code and here is the idea - if anyone can contribute to it. It's like a social media platform where users enter the site and the is_user_logged_in parameter ...

The value of form.__EVENTTARGET is undefined in JavaScript

When using my webforms.app on .net 4.0 and calling _doPostBack via aspx code <%= GetPostBackReference() %>; in the code behind protected string GetPostBackReference() { return Page.ClientScript.GetPostBackEventReference(ReloadThePanel,"null" ...

Submitting an AJAX request to upload an XML file and an image file simultaneously in one call

I need to send both an uploaded image and an uploaded XML file to a PHP file using a single AJAX call. I have tried using two form data instances, but I am not sure if this is the correct approach. <input type="file" class="form-control-file" name="fil ...

Using Angular2, you can dynamically assign values to data-* attributes

In my project, I am looking to create a component that can display different icons based on input. The format required by the icon framework is as follows: <span class="icon icon-generic" data-icon="B"></span> The data-icon="B" attribute sp ...

Creating a new dynamic page can be achieved by clicking on a dynamically generated link. Would you like to learn how to do that?

Recently, I developed a custom API using Node.js to retrieve information about blogs from Medium.com. The API currently provides: The author/main picture of the article Title A link to the article on medium.com (redundant) The entire article text in the ...

Experience the advanced NgPrime p-dropdown template with templating, filtering, and a clear icon that collapses when wrapped within a form element

Check out this link for a demo Whenever I enclose the code below in a </form> element, the drop-down menu collapses. <h5>Advanced with Templating, Filtering and Clear Icon</h5> <p-dropdown [options]="countries" [(ngModel)]=& ...

Steps for building a Bootstrap grid of thumbnails

I need to display an unknown number of thumbs. Below is a sample of the HTML rendered: <div class="row-fluid"> <ul class="thumbnails"> <li class="span3"> <a href="#" class="thumbnail"> &l ...

Combining two HTML tables using jQuery/JavaScript

My table is displayed below: <table id="first" class="merge"> <tr> <td>Mick Jagger</td> <td>30</td> <td>50</td> <td>10</td> </t ...

Retrieve information from individual XML nodes in a parsed string

When making an Ajax call to retrieve XML data from a different domain using a PHP proxy to bypass cross-origin restrictions, I am unable to extract the values from the XML nodes and display them in my HTML tags. Despite no errors showing up in the browser ...