jQuery plugin stops functioning properly following the use of the jQuery display block method

In my project, I am exploring the use of divs as tabs with jQuery. Within these divs, I also want to incorporate another jQuery plugin. Currently, I have manually created these div tabs using jQuery and set the default style for second and subsequent divs to display: none either through pure css or a specified class name. However, when I change the display property of the divs to display: block, the second jQuery plugin within the div stops functioning.

The issue arises when attempting to hide the divs without utilizing pure css or a class name, as there is a noticeable delay (approximately 1 second) when the HTML page loads. Are there any alternative suggestions to address this delay apart from implementing a loading icon?

I have experimented with using bootstrap tabs, but unfortunately, the problem persists. How can I effectively utilize a pre-defined class or pure css to initially set the visibility of the divs to display: none, allowing for their subsequent display upon client interaction while ensuring the proper functionality of the second jQuery plugin embedded within?

<div class="tabs-wrapper">
    <div class="tab-container tab1">
        some code here
    </div>
    <div class="tab-container tab2 d-none">
        <div>
            second jquery plugin code here
        </div>
    </div>
</div>

<button id="btn1">Btn1</button>
<button id="btn2">Btn2</button>

<script>
$(function(){
    $('#btn1').click(function () {
        $('.tabs-wrapper .tab1').addClass('d-none');
        $('.tabs-wrapper .tab2').removeClass('d-none');
    });

    $('#btn2').click(function () {
        $('.tabs-wrapper .tab2').removeClass('d-none');
        $('.tabs-wrapper .tab1').addClass('d-none');
    });
});
</script>

Issues arise when attempting to hide divs using a css class or inline styling, as the second jQuery plugin (e.g., sliders) ceases to function correctly.

Answer №1

Kindly provide a sample of the internal code you wish to execute. However, simply toggling the visibility of a div will not automatically trigger the execution of code within it. You need to handle an event when the div is displayed. Moreover, there seems to be an issue in your code snippet. Below are two possible solutions:


<style>
    .d-none
    {
        display:none;
    }
</style>
<body>

<div class="tabs-wrapper">
    <div class="tab-container tab1">
        // code for tab 1 goes here
    </div>
    <div class="tab-container tab2 d-none">
        <div>
            // code for tab 2 with jQuery plugin
        </div>
    </div>
</div>

<button id="btn1">Btn1</button>
<button id="btn2">Btn2</button>

<script>
// Option 1
$(function(){
    $('#btn1').click(function () {
        $('.tabs-wrapper .tab1').addClass('d-none');
        $('.tabs-wrapper .tab2').removeClass('d-none');
    });

    $('#btn2').click(function () {
        $('.tabs-wrapper .tab1').removeClass('d-none');
        $('.tabs-wrapper .tab2').addClass('d-none');
    });
});
/*  Option 2
$(function(){
    $('#btn1').click(function () {
        $('.tab1').hide();
        $('.tab2').show();
    });

    $('#btn2').click(function () {
        $('.tab2').hide();
        $('.tab1').show();
    });
});
*/

</script>

Answer №2

$('#btn1').click(function () {
            $('.tabs-wrapper .tab1').addClass('d-none');
            $('.tabs-wrapper .tab2').removeClass('d-none');
        });
    
        $('#btn2').click(function () {
            $('.tabs-wrapper .tab1').removeClass('d-none');
            $('.tabs-wrapper .tab2').addClass('d-none');
        });
.d-none
{
    display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="tabs-wrapper">
    <div class="tab-container tab1">
        some code here
    </div>
    <div class="tab-container tab2 d-none">
        <div>
            second jquery plugin code here
        </div>
    </div>
</div>

<button id="btn1">Btn1</button>
<button id="btn2">Btn2</button>

You may also attempt this method

$(function(){
    $('#btn1').click(function () {
        $('.tabs-wrapper .tab1').addClass('d-none');
        $('.tabs-wrapper .tab2').removeClass('d-none');
    });

    $('#btn2').click(function () {
        $('.tabs-wrapper .tab1').removeClass('d-none');
        $('.tabs-wrapper .tab2').addClass('d-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

Error: Unable to modify the value of a protected property '0' in the object 'Array'

I am facing a challenging issue with implementing a Material UI slider in conjunction with Redux. Below is the code for the slider component: import { Slider } from '@material-ui/core' const RangeSlider = ({handleRange, range}) => { ...

The main page wrapper will be centered on all devices except for tablets, where it will appear

I have set up a main container, #main, for my page content with a central position. It is positioned absolutely to create some space above the container. Centering is achieved using negative margins and left:50% instead of margin:auto. However, the issue a ...

Uploading images with Laravel and VueJS

I am having trouble with Vuejs image upload. My backend is Laravel, but for some reason the images are not being sent to the Controller. <form method="POST" class="form-horizontal" role="form" v-on:submit.prevent="updateProduct(editProduct.id)" en ...

Struggling with handling multiple jQuery AJAX requests on a single webpage

Greetings, I am currently working on a PHP Project that involves loading requests using ajax. Below are the two functions in question: Function One: Upon clicking the showNotify event, an ajax request is made to retrieve content with a spinner displayed u ...

What's the issue with this code not functioning correctly?

puts.php (JSON) { "image":[ { "name":'<div id="yes">Hi!</div>' } ] } process.php <HTML> <HEAD> <TITLE>Process</TITLE> <script type="text/javascript" s ...

Tips for avoiding a form reload on onSubmit during unit testing with jasmine

I'm currently working on a unit test to ensure that a user can't submit a form until all fields have been filled out. The test itself is functioning correctly and passes, but the problem arises when the default behavior of form submission causes ...

Unlocking the Power of ReactJS: Passing Values in Material UI for Advanced JSON Structures

How can I access complex objects in the GRID component of material UI? Specifically, I am trying to access the ami_info.account, but it only displays Undefined in the UI. var columns = [ { field: 'id', headerName: 'ID', width: 90, ...

Displaying videos in full screen using HTML5

I am attempting to create a fullscreen webpage with a video using the following code: <video id="backgroundvideo" autoplay controls> <source src="{% path video, 'reference' %}" type="video/mp4"> </video> ...

Condition formulation using dynamic dust JS equality

I'm struggling with a seemingly simple task - dynamically changing the @eq condition. The example provided shows a default render, but what I really need is to allow user input to change it to either "Larry" or "Moe". You can view my code on jsfiddle ...

Nuxt Js - Ensuring script is only loaded once during the initial page load

I already have a static website design, but now I'm converting it to Nuxt.js to make it more interactive. After running my Nuxt server with "npm run build...npm run start," the scripts load and my carousel/slides work fine. However, when I navigate to ...

Check if the provided arguments in JavaScript are arrays and, if they are, combine them into a single large array

My array variables are as follows: const gumBrands = ['orbit', 'trident', 'chiclet', 'strident']; const mintBrands = ['altoids', 'certs', 'breath savers', 'tic tac']; Presen ...

The TypeScript inference feature is not functioning correctly

Consider the following definitions- I am confused why TypeScript fails to infer the types correctly. If you have a solution, please share! Important Notes: * Ensure that the "Strict Null Check" option is enabled. * The code includes c ...

What is the rationale behind assigning a random value to the `(keyup)` event in order to update template local variables in Angular2?

To update #box in <p>, I need to give a random value to the (keyup) attribute. Here's an example: <!-- The value on the right of equality sign for (keyup) doesn't matter --> <input #box (keyup)="some_random_value" placeholder ...

What is the reasoning behind placing CDN links at the bottom of the index file?

What is the reason for placing CDN links for AngularJS file at the end of the index page? I initially placed it at the top of the file and it worked fine. Is there a significant difference between these two placements? ...

Requesting the user to repeatedly input their birth year until it is less than the current year

Can anyone help me figure out how to display a prompt until the user enters a birth year that is less than the current year? I've tried using a loop in my code, but I'm having trouble getting it right. Any assistance would be greatly appreciated. ...

What is the best way to manage classNames dynamically in React with Material-UI?

I am wondering how to dynamically add and remove classes from an img tag. My goal is to change the image automatically every 2 seconds, similar to Instagram's signup page. I am struggling to achieve this using the material-ui approach. Below is a snip ...

Is there a feature similar to Google/YouTube Insight called "Interactive PNGs"?

Recently, I have been exploring the YouTube Insight feature and I am intrigued by how their chart PNGs are generated. When you view the statistics for a video, the information is presented in interactive PNG images. These images seem to be entirely compos ...

Ways to update a component upon becoming visible in Angular

Within my Angular 4.3.0 project, the header, left panel, and right panels are initially hidden on the login page until a successful login occurs. However, once they become visible, the data is not pre-loaded properly causing errors due to the ngOnInit meth ...

Why are link elements that generate hyperlinks important?

I am confused about the purpose of using link tags to create hyperlinks. Is there a reason to include code like <link rel="author" href="about.html">, <link rel="next" href="next.html">, or <link rel="help" href="help.html">? Since this ...

Chosen Dropdown selection - Retrieve/Receive information

One of the challenges I'm facing involves a dropdown list on my web form that contains various dates. My main query is: How can I retrieve data for the selected date from a MySQL database and present it to the user? Additionally, if the chosen date do ...