The tab panels are failing to function properly due to the maxcdn CSS integration

I am struggling to make my basic JQuery code work, specifically to show and hide panels when a list item in the menu is clicked. The CSS I am using is from max cdn, so I'm unsure if I need to create my own CSS file for this.

    <!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/sandstone/bootstrap.min.css"></script>
        <script src ="PhpProject1/jquery.js"></script>
        <script>
            $(document).ready(function() {
             $('.tabs a').click(function(){
                var $this =$(this);
                $('.panel').hide();
                $('.tabs a.active').removeClass('active');
                $this.addClass('active').blur();});
            }); // end ready
        </script>
    </head>
    <body>
        <div id="tabContainer">
            <h2>Tab Heading</h2>
            <div class=col-xs-3">
                <ul class="nav nav-tabs" role="tablist">
                <li  class="active"><a href="#">Home</a></li>
                <li><a href="#">Menu1</a></li>
                <li><a href="#">Menu2</a></li>
                <li><a href="#">Menu3</a></li>
              </ul>
                <div id ="panel1" class="panel panel-primary">
                    <div class="panel-heading">   
                     #panel1
                    </div>
                    <div class="panel-body">content
                    </div>    
                </div>
                <div id ="panel2" class="panel panel-primary">
                    <div class="panel-heading">   
                     #panel2
                    </div>
                    <div class="panel-body">content
                    </div>    
                </div>
                <div id ="panel3" class="panel panel-primary">
                    <div class="panel-heading">   
                     #panel3
                    </div>
                    <div class="panel-body">content
                    </div>    
                </div>
            </div>
    </body>
</html>

Answer №1

The main issue lies in the presence of a script tag linking to CSS.

Instead of this:

<script src="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/sandstone/bootstrap.min.css"></script>

it should be like this:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/sandstone/bootstrap.css">

Even after correcting that, there are other problems present. For instance, $('.tabs a').click(... does not function because there is no element with the "tabs" class on the page. Changing it to $('.nav-tabs a').click(... would help, but further adjustments are still required.

UPDATE

Here's a possible solution to resolve the issues. Updated script:

$(document).ready(function () {
    $('.nav-tabs li').click(function (e) {
        e.preventDefault();
        var $this = $(this);
        $('.panel').hide();
        $('#' + $this.find('a').attr('href')).show();
        $('.nav-tabs .active').removeClass('active');
        $this.addClass('active').blur();
        return false;
    });
}); // end ready

New tabs arrangement:

<ul class="nav nav-tabs" role="tablist">
    <li class="active"><a href="home">Home</a></li>
    <li><a href="panel1">Menu1</a></li>
    <li><a href="panel2">Menu2</a></li>
    <li><a href="panel3">Menu3</a></li>
</ul>

Updated panels layout:

<div id="home" class="panel panel-primary">
    <div class="panel-heading">
        #home
    </div>
    <div class="panel-body">
        Home content
    </div>
</div>
<div id ="panel1" class="panel panel-primary" style="display: none">
    <div class="panel-heading">   
        #panel1
    </div>
    <div class="panel-body">content
    </div>    
</div>
<div id ="panel2" class="panel panel-primary" style="display: none">
    <div class="panel-heading">   
        #panel2
    </div>
    <div class="panel-body">content
    </div>    
</div>
<div id ="panel3" class="panel panel-primary" style="display: none">
    <div class="panel-heading">   
        #panel3
    </div>
    <div class="panel-body">content
    </div>    
</div>

A few key points to consider:

  1. I assigned the click event on the li elements instead of the a tags as the "active" class pertains to these.
  2. The href attribute of the anchor tags determines which panel to display.
  3. A new "home" panel was created and all others were initially set to display:none.

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

"Enhance your JQGrid experience with the Free-JQGrid Multiselect toolbar, which automatically refresh

I am facing an issue with refreshing my drop-down filters in the grid after making a server call to reload it. Initially, upon loading the page, the grid displays correctly with drop-down filters excluding quotes in canceled or expired status. However, w ...

The hover feature in jQuery may encounter issues when implemented with Angular's ng-repeat directive on objects

I have successfully implemented a jQuery hover function: $('.deleteButton').hover(function(){ $(this).css('opacity', '1'); }, function(){ $(this).css('opacity', '.5'); }); The function was functi ...

"Utilize jQuery AJAX Promises to Trigger Custom Exceptions that can be Handled by the Outer fail() Function

Imagine having a function that yields a promise. The promise returned is scrutinized by other functions to determine how to manage the .fail() condition. function refreshTimeline() { var promise = ajaxMethod1() .then (function (data) ...

Creating messages from an array using vanilla JavaScript or jQuery

I have an array of objects containing messages that I want to display on my webpage, but I'm unsure how to approach it. [{"message":"dwd","user":"csac","date":"07.04.2021 20:46"}, {"mes ...

Can Vuejs functions be triggered using a jQuery event trigger?

I am currently attempting to trigger a change event within a Vue component. Within the component template, there is a select element. When I try to use jQuery to trigger a change event on this element, the Vue component does not seem to detect it. Here i ...

Using JQuery to manipulate `this`, its children, its siblings, and more

Can anyone help me determine the most effective way to send data from a get request to a specific div? My challenge is to send it only to a div that is related to the one triggering the action. For example, when I click on the message_tab, I want to send t ...

The issue with disappearing HTML elements on input focus was resolved in iOS 8.3

I encountered a peculiar issue that bears resemblance to the fixed element problems on iOS highlighted on Stack Overflow, but with a unique twist. This anomaly only occurs when the parent document is scrollable and involves the use of an iframe. Below is a ...

Prevent the loading of multiple jQuery versions

Currently on Yii 2.0.9, I am encountering an issue with conflicting jquery versions. The framework loads jquery 2.2.4 without any issues, but the krajee extensions are introducing jquery 1.7.2 which I do not prefer. Is there a way to modify my AppAssets ...

What is the process of uploading files using Ajax?

Can anyone help me with submitting a form using ajax that contains images? I have this code snippet which logs an object called "FormData" in the console, but I'm not sure how to properly use it or retrieve it. Is this the correct approach? $("#form_ ...

Enhancing your JQuery Select2 plugin by incorporating a Checkbox feature

I am currently utilizing the jQuery select2 plugin to enable multiple selections. My goal is to incorporate a checkbox for each selectable option. Depending on whether the checkbox is checked or unchecked, the dropdown option should be selected accordingl ...

Something is amiss with the PHP email validation functionality

I have been facing issues with the functionality of my form that uses radio buttons to display textboxes. I implemented a PHP script for email validation and redirection upon completion, but it doesn't seem to be functioning correctly. The error messa ...

updating the background image in CSS after uploading a photo using Ajax

Whenever I click on an element, it triggers an input dialog that allows me to upload a photo. The server then responds with a JSON OBJECT containing 4 items: NAME, TYPE, HEIGHT, WIDTH. My goal is to refresh the element's background with the newly upl ...

Looking to store all JSON data into an array using AJAX with jQuery

Check out this piece of code: var newsContent = []; // ** GLOBAL ** $(document.body).click(function() { $.ajax({ type: "POST", url: "http://example.com/newsfeed.php", success: function (data) { ...

Troubleshooting problem with customized Bootstrap 5 form-switch functionality

Currently, I am developing a website utilizing Bootstrap 5. However, I have encountered a problem with form switches when using a customized version of Bootstrap 5.1.3 Interestingly, when I include Bootstrap 5.1.3 via CDN, the form switches display correc ...

Updating Page Content with JQuery AJAX Post Without Page Reload

So I realize I may be asking a question that has been asked before, but as a complete beginner with AJAX jQuery, I'm not sure what to search for. I've tried searching without success, so please bear with me. I have a PHP WordPress site that defa ...

Tips for populating text box values using AngularJSWould you like to learn how

I have a UI table in HTML where I display records fetched from the database using AngularJS. Scenario: There is a text box where I can select an ID and the related fields are displayed in the table mapped for that particular ID. <td>Name</td> ...

Searching for li elements that contain text values - a guide

I have a list of letters and I want to filter out any values that contain the text entered by the user in a textbox. Here is the design: Search List: <input type="text" id="txtSearch" /> <ul> <li>Coffee1</li> <li>Coffe ...

Personalizing the pop-up window using window.open

When clicking a hyperlink on a page, I need to open multiple pop-up windows. To achieve this, I must use the Window.open function instead of showModalDialog. However, I have noticed that the appearance is not satisfactory when using Window.open. (Essentia ...

Troubleshooting issue with jquery.i18n.properties functionality

I am encountering an issue with implementing jQuery internationalization. I have included the files jquery.i18n.properties.js, jquery.i18n.properties-min.js, and jquery-min.js in my resources folder. In my jsp, I have added the following code: <script ...

I am looking to showcase images beside individuals' names or photos on my website in a vertical arrangement, similar to how it is done on Facebook

Looking for suggestions on how to display images uploaded by users on my webpage in a way similar to Facebook, with the user's photo displayed beside each image. Any recommendations or website links would be greatly appreciated. Thanks, Santosh Sahu ...