Utilizing jQuery to link item to content div ID in a seamless connection

Trying to design a user-friendly, multi-tabbed navigation system within a single webpage. This is how the content structure looks:

HTML

<ul id="nav">
    <li><a href="tab1">Tab 1</a></li>
    <li><a href="tab2">Tab 2</a></li>
    <li class="active"><a href="tab3">Tab 3</a></li>
</ul><!--/#nav-->

<div id="content-box">
    <div id="tab1">
        <!--content from tab1-->
    </div><!--/#tab1-->

    <div id="tab2">
        <!--content from tab2-->
    </div><!--/#tab2-->

    <div id="tab3">
        <!--content from tab3-->
    </div><!--/#tab3-->

</div><!--/#content-box-->

JQuery:

$("#nav li").on("click", function(event){
        $("#nav li").removeClass("active");
        $(this).addClass("active");
        $("#content-box").children().hide();
    });

Stuck at linking the li with the active class to the respective div. All tab divs are hidden by default and I've created a class called .activeSlide to toggle visibility.

Answer №1

utilize html 5 data attributes for connecting the div

html

<ul id="nav">
  <li data-content="tab1">Tab 1</li>
  <li data-content="tab2">Tab 2</li>
  <li data-content="tab3" class="active">Tab 3</li>
</ul><!--/#nav-->

jquery

$("#nav li").on("click", function(event){
    $("#nav li").removeClass("active");
    $(this).addClass("active");
    $("#content-box").children().hide();
    $('#'+ $(this).data('content')).show(); 
    //OR
    $("#content-box").children().removeClass('activeSlide');
    $('#'+ $(this).data('content')).addClass('activeSlide');
});

check out the fiddle here

updated

include this in the ready function

 $(document).ready(){
     $("#content-box").children().hide();
    $("#nav li").on("click", function(event){
      ...... //above function goes here
    });
  
});

see the updated fiddle here

but I suggest using jquery ui tab instead of starting from scratch... :)

Answer №2

After your click action is completed:

$('#' + $('a', this).attr('href')).show();

I advise that if you anticipate a large amount of content on one webpage, it may be beneficial to implement a method for users to bookmark specific content. The current design may present challenges in bookmarking.

Answer №3

Transform to this:

HTML

Get rid of the ids tab1, tab2, and tab3. Replace them with the class "tab" instead.

For example,

<div class="tab">

instead of using id="tab1", "tab2", and "tab3"

JAVASCRIPT

$("#nav li")
    .on("click", function(event){
            var _index = $(this).index();
            $(".tab").hide();
            $(".tab:eq("+_index+")").show();

            $(this).siblings().removeClass('active');
            $(this).addClass('active');
     });

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

Converting JSON data into a dictionary within a Web API controller

I am facing an issue with a JSON string I have: '{"1":[1,3,5],"2":[2,5,6],"3":[5,6,8]}' My goal is to send this JSON data to the Web Api Controller without making any changes using an ajax request: $.ajax({ type: "POST", url: ...

Creating boxes with equal heights in HTML and CSS

I have created this menu using a combination of html and css. The layout consists of 3 boxes, each with different content. My goal is to ensure that all boxes are the same height within the design. However, due to specific responsive requirements, I cannot ...

Struggling with this CSS is really getting to me

I am struggling to create a modal with the tools and close button appearing on the same line. It should look similar to the image below: var modal = document.getElementById('myModal'); var btn = document.getElementById("myBtn"); var span = doc ...

Tips for effectively locating code within a web browser's code base

Struggling as a new programmer in a new job with a fresh codebase, I am finding it difficult to connect the code to the webpage I am currently on. What methods can I use to locate the relevant HTML in the codebase that is being rendered in the web browser? ...

Concealing the title of a page on GitHub pages while keeping it visible on the browser tab

My personal website is currently hosted on GitHub pages and it uses the Minimal Mistakes theme which I forked from the academicpages GitHub repository. When creating pages under the _pages directory, we follow a basic format: --- permalink: / title: " ...

Is your Jquery AJAX request not functioning asynchronously as expected?

I recently started delving into web design and am just beginning to explore jQuery. In my quest to implement a star rating system on my website, I encountered an issue where the user needs to refresh the entire page in order to see the updated star ratings ...

Tips for effectively incorporating CSS classes with Booststrap and Wagtail

Currently, I am utilizing a Bootstrap theme and trying to place up to three cards horizontally on a page at one specific location. Here is the HTML code: <div class="container"> <h1 class="text-center">{{ self.title }}</h1> <d ...

What method should I use to target a specific element while loading content with AJAX in jQuery?

Is there a way to specify an element that will display content retrieved from a specific page URL within the code below? $(function () { $("a[rel='sort']").click(function (e) { //e.preventDefault(); /* if uncommen ...

The issue with my Wordpress frontend ajax form not functioning as expected

I've been struggling with this issue for hours now. I'm still new to Ajax, so I might be overlooking something simple. Please help me out before I pull all my hair out! In my functions.php file // Setting up the Ajax Quote Box function ajax_quo ...

determining the 'top' and 'right' coordinates upon clicking

I encountered an issue with a table having a drop-down menu that was getting cut off when opened on the last line. After some experimentation, I came up with a solution to detach the drop-down menu from its parent element and then reposition it by setting ...

There is a single successful match for the regex

I am facing an issue with my regex that is supposed to rewrite [img]foo.bar[/img] to , which it successfully does. However, the problem arises when there are two or more instances of [img]url.ext[/img] in the string. Instead of matching each one individual ...

Creating a responsive navigation menu using Bootstrap 4 by merging data from various MySQL tables

I am struggling to create a bootstrap 4 nav-menu using the provided SQL query and code snippets. Below are the tables: TABLE menu -------------------------------------- | id | title | url | | 1 | Home | index.php | | 2 | M ...

Challenges with the Placeholder and Text Alignment in Angular Material's Text Area

I am currently facing an issue with a textarea in Angular, which is displayed as shown below: https://i.sstatic.net/jjUlT.png My Angular code for the textarea is as follows: <div *ngIf="whetherDisplay()" class="textarea-text"> <mat-form-field ...

having difficulty choosing the dropdown with a single index value

Having trouble selecting just one option from a list of multiple options in the dropdown. The index value is showing as 1 so I am not able to make a selection. ...

Adjust the sequence of the series in dimple's multi-series chart using interactive features

My latest project involves a unique dimple interactive chart featuring two series: a pie series and a line series based on the same dataset. You can view the chart at dimplejs.org/advanced_examples_viewer.html?id=advanced_interactive_legends Check out the ...

Chrome failing to recalculate the width of an element as required

When attempting to size an element's width based on its height, I discovered what seemed like a clever solution using the transparent img trick: http://jsfiddle.net/6yc8a2yj/ The pink div displays correctly at a 1:2 ratio, but it fails to resize whe ...

Adding new information to an associative or multidimensional array

In my web application, there is a functionality that involves selecting a Country which then populates the City select dropdown. Once a city is selected, a radius is added surrounding that particular city. var countrySet = new Array; var citySet = new Arr ...

Struggling to apply a background image to a specific section with the help of Tailwind CSS in a Next.js project using TypeScript

I've been struggling to add a background image to a section despite trying various solutions I found on Stackoverflow. Here is the current code that I have: import Image from '../../public/images/header2.jpg'; const Home: React.FC = () ...

NG-Bootstrap Navigation Bar- dynamic hamburger icon animation

I have been trying to implement an animated hamburger animation on the navbar using ng-bootstrap and Angular, but so far I have had no success. While the Navbar does collapse as expected, it lacks the animation and transformation into an 'x'. I w ...

Search box enlargement obstructing navigation bar links

I've been experimenting with a Javascript feature that starts with a search monocle icon. When clicked, the search box expands over the navigation bar. However, I'm running into an issue where even when the search box is not covering the navbar, ...