Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
How to toggle div content when clicking on different navigation links?

I want to have my whole website as one page.

When clicking on different links in the navigation bar, the content should cross-fade from the old to the new one.

Example: I am on index.html#home and click on About. This should cross-fade between the divs #home and #about.

Using W3CSS and jQuery I managed to hide the content like this:

function hideContent() {
        $(".content-active").addClass("w3-hide", "content-inactive");
        $(".content-active").removeClass("w3-show", "content-active");
      };
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#home" class="w3-bar-item w3-hide-small" style="width:16.5%">Home</a>
<a href="#about" class="w3-bar-item w3-hide-small" style="width:16.5%" onclick="hideContent()">About</a>
<div id="home" class="w3-container w3-green w3-show content-active" style="height:50px"></div>
<div id="about" class="w3-container w3-red w3-hide content-inactive" style="height:50px"></div>

I am not able to show the new content though. I wanted to add an onclick function to the navigation link and then extract the href-value to again change the class names for the div that has the ID of the href-value.

But doing this on <a href="#" onclick="showContent()></a> returns undefined:

function showContent() {
    var hrefValue = $(this).attr("href");
    alert(hrefValue);
  }

I guess this is not in the proper scope of <a> here. Am I assuming correctly?

What would be an easy way to show the content otherwise?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Rather than embedding your function in html, add an id to your anchor element and remove the onclick attribute:

<a href="#about" id="aboutclick" class="w3-bar-item w3-hide-small" style="width:16.5%">About</a>

Then add an event handler to the element by using the following code to show or hide and do the other things you need to do

$('#aboutclick').click(function(event) {
  ...
});

Then event.target will give you the element you need and you can use this to get attribute values. If you're handling click events then don't forget to prevent the default action if needed.

More details here

about 4 years ago · Juan Pablo Isaza Report

0

Here you go

$("#aboutclick").click(function(event) {
  const targetDiv=$(event.target).attr('href');
  hideContent();
  $(targetDiv).addClass("w3-show", "content-active");
});

function hideContent() {
        $(".content-active").addClass("w3-hide", "content-inactive");
        $(".content-active").removeClass("w3-show", "content-active");
      };
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#home" class="w3-bar-item w3-hide-small" style="width:16.5%">Home</a>
<a href="#about" id="aboutclick" class="w3-bar-item w3-hide-small" style="width:16.5%">About</a>
<div id="home" class="w3-container w3-green w3-show content-active" style="height:50px">Home Div</div>
<div id="about" class="w3-container w3-red w3-hide content-inactive" style="height:50px">About Div</div>

about 4 years ago · Juan Pablo Isaza Report

0

Thank you so much! It took me a while to digest your answer and apply it properly to my website layout. But I think I found a good way to do it now. At least it does what it's supposed to do and I managed to separate HTML from jQuery. Please don't hesitate to tell me if there is anything wonky going on:

$(document).ready(function() {
    $(".navItemClick").click(function(event){
      const targetDiv = $(event.target).attr("href");
      event.preventDefault();
      $(".content-active").hide("slow", function() {
        $(this).removeClass("content-active");
        $(this).addClass("content-inactive");
        $(targetDiv).show("slow");
        $(targetDiv).removeClass("content-inactive");
        $(targetDiv).addClass("content-active");
      });
    });
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!