The functionality of Jquery to update the display styling using CSS is not functioning properly

Here is the code snippet:

<script type="text/javascript>
    $(window).load(function(){
            debugger
            $('#comp').ready(function() {
                $('#QV101').css("display", "inline");
                $('#QV102').css("display", "none");
                $('#QV103').css("display", "none");
            });

            $('#comp').on('click', function(event) {
                debugger
                $('#QV101').css("display", "inline");
                $('#QV102').css("display", "none");
                $('#QV103').css("display", "none");  
            });
            $('#mes').on('click', function(event) {
                debugger
                $('#QV101').css("display", "none");
                $('#QV102').css("display", "inline");
                $('#QV103').css("display", "none");

            });
            $('#hora').on('click', function(event) {
                debugger
                $('#QV102').css("display", "none");
                $('#QV101').css("display", "none");
                $('#QV103').css("display", "inline");

            });
            debugger

        });
</script>

The issue I am facing is that when I click on other buttons, such as 'mes' or 'hora', the elements do not display despite the CSS changes being reflected in the web console. I prefer to avoid using 'hidden' as it creates an unsightly gap on the screen.

The HTML code contains values like #QV... which are related to Qlik and display a chart.

Comparativa Consumo mes consumo hora Consumo dia

                <div id="QV101" class="col-md-12 qvobject">    
                </div>
                <div id="QV102" class="col-md-12 qvobject">       
                </div>
                <div id="QV103" class="col-md-12 qvobject">

                </div>

            </div>



            <div class="row">
                <div id="QV114" class="col-md-2 qvplaceholder">

                </div>
                <div id="QV115" class="col-md-2 qvplaceholder">

                </div>
                <div id="QV117" class="col-md-2 qvplaceholder">

                </div>

            </div>
        </div>
    </div>

Thank you!

Answer №1

$('#QV101').addClass('show');
$('#QV101').siblings(':not(button)').addClass('hide')



$('#comp').on('click', function(event) {
  $('#QV101').siblings(':not(button)').removeClass('show');
  $('#QV101').addClass('show');
  $('#QV101').siblings(':not(button)').addClass('hide');
});
$('#mes').on('click', function(event) {
  $('#QV102').siblings(':not(button)').removeClass('show');
  $('#QV102').addClass('show');
  $('#QV102').siblings(':not(button)').addClass('hide');
});
$('#hora').on('click', function(event) {
  $('#QV103').siblings(':not(button)').removeClass('show');
  $('#QV103').addClass('show');
  $('#QV103').siblings(':not(button)').addClass('hide');

});
.hide {
  display: none
}
.show {
  display: block
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="QV101">1</div>
<div id="QV102">2</div>
<div id="QV103">3</div>


<button id="comp">comp</button>
<button id="mes">mes</button>
<button id="hora">hora</button>

  1. integrated :not(button) to exclude buttons (also can utilize sibling(div))
  2. utilize .siblings() to retrieve all div elements.
  3. modify the class attribute based on the button that is clicked.

Answer №2

Here is my recommended approach that streamlines the code and makes it easier to maintain as outlined in the comments above. You can find the JSFiddle link below:

Below is the updated code snippet:

$(function(){

    $('#comp').ready(function() {
    $('#QV101').css("display", "inline");
    $('#QV102').css("display", "none");
    $('#QV103').css("display", "none");
  });

  $('#comp').on('click', function(event) {
    $('.toggleDiv').hide();
    $('#QV101').css("display", "inline");    
  });
  $('#mes').on('click', function(event) {
    $('.toggleDiv').hide();    
    $('#QV102').css("display", "inline");
  });
  $('#hora').on('click', function(event) {
    $('.toggleDiv').hide();    
    $('#QV103').css("display", "inline");
  });

});

Click here for the Working Fiddle

Answer №3

Check out this code snippet to see if your code is functioning correctly:

$(window).load(function(){
  debugger
  $('#comp').ready(function() {
    $('#QV101').css("display", "inline");
    $('#QV102').css("display", "none");
    $('#QV103').css("display", "none");
  });
  $('#comp').on('click', function(event) {
    debugger
    $('#QV101').css("display", "inline");
    $('#QV102').css("display", "none");
    $('#QV103').css("display", "none");  
  });
  $('#mes').on('click', function(event) {
    debugger
    $('#QV101').css("display", "none");
    $('#QV102').css("display", "inline");
    $('#QV103').css("display", "none");

  });
  $('#hora').on('click', function(event) {
    debugger
    $('#QV102').css("display", "none");
    $('#QV101').css("display", "none");
    $('#QV103').css("display", "inline");

  });
  debugger
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="comp">comp</button>
<button id="mes">mes</button>
<button id="hora">hora</button>
<div id="QV101">QV101</div>
<div id="QV102">QV102</div>
<div id="QV103">QV103</div>

<script type="text/javascript">

</script>

Answer №4

I managed to find a solution for the issue. Below is the code snippet that helped me resolve it:

<script type="text/javascript">
$(window).load(function(){
debugger
 $('#QV101').css("display", "inline");
 $('#QV102').css("display", "none");
 $('#QV103').css("display", "none");

$('#comp').on('click', function(event) {
 debugger
 $('#QV101').css("display", "inline");
 $('#QV102').css("display", "none");
 $('#QV103').css("display", "none");  
});
$('#mes').on('click', function(event) {
debugger
 $('#QV101').css("display", "none");
 $('#QV102').css("display", "inline");
 $('#QV103').css("display", "none");

});
$('#hora').on('click', function(event) {
debugger
 $('#QV102').css("display", "none");
 $('#QV101').css("display", "none");
 $('#QV103').css("display", "inline");

});
debugger
});
    </script>

The issue I encountered was related to the Qlik chart not displaying initially but only after scrolling down a bit. This specific chart was created outside of the Qlik app, and that seemed to be causing the delay in rendering.

I appreciate all the assistance provided so far!

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

What is the best way to show a background image behind the heading text?

I have a strip of images that I want to use as a background for my heading. However, I am encountering an issue where the first and last images of the strip are not displaying properly. Additionally, the image border appears straight instead of following ...

Changing the date format for the week view feature on the calendar

I am currently utilizing the FullCalendar plugin for jQuery in my latest project. When viewing the weekly layout in FullCalendar, there is a row displaying the date in the format of: Sunday 9/6, Monday 9/7, Tuesday 9/8 and so forth... My goal is to actua ...

What is the best way to integrate my company's global styles CDN for development purposes into a Vue cli project using Webpack?

After attempting to import through the entry file (main.js)... import Vue from 'vue' import App from '@/App' import router from '@/router/router' import store from '@/store/store' import BootstrapVue from 'boot ...

Choose a Range of DOM Elements

My challenge is to select a range of DOM elements, starting from element until element. This can be done in jQuery like this: (Source) $('#id').nextUntil('#id2').andSelf().add('#id2') I want to achieve the same using JavaScr ...

Resize the shape of an image's polygon

Is there a way to independently scale a specific polygon of an image on hover? For example, I have a world map and I would like to enlarge a country when hovering over it, then return it to its original size when no longer hovering. I am familiar with us ...

Fade In and Contemplate CSS, Not Effective

Hey there! I'm trying to achieve a similar effect like the one shown in http://designshack.net/tutorialexamples/HoverEffects/Ex5.html. The issue is that the reflection of the image isn't displaying as it should. I've tried inspecting the ele ...

Choosing groupings of courses

I am looking to dynamically load divs with different combinations of classes from an external file using jQuery's load function, but I am struggling with grouping them correctly. $("#somediv").load("somefile.html .class1"); // loads all divs with c ...

Manipulate the hover effect of an element using jQuery

I currently have a webpage with 6 menu buttons, each with its own background for the hover event using CSS. While everything is working well, I now have the requirement to make the background static when a page is selected. Is it feasible to achieve this u ...

Inheriting the viewBox attribute with SvgIcon

I have a collection of unique custom SVGs, each with its own distinct viewBox. First Custom SVG <svg viewBox="-4 -4 24 24"><path something/></svg> Second Custom SVG <svg viewBox="-5 -7 24 24"><path something ...

Showing NaN/NaN when no results were found in jQuery DataTables

I found a jQuery data tables function on Stack Overflow that I am using. Here is the code: var oTable = $('#test').dataTable( { "aoColumnDefs": [ { "bSortable": false, "aTargets": [ 0 ] } ], "aaSorting": [[1, 'asc'] ...

Is there a way to prevent special characters from being typed without affecting the arrow keys?

To allow input of numbers from 0 to 9 as well as navigation keys such as up, down, left, right, delete, tab, home, and end, including modifier keys like alt and ctrl, this code is compatible with Chrome and Firefox browsers. $('.commonNumber').k ...

Utilizing JSON data for Autocomplete with Ajax and Jquery

I have been working on setting up my Jquery UI autocomplete field to pull data from an ajax connection. Here is the progress I've made so far: $("#mainIngredientAutoComplete").autocomplete({ source: function (request, response) { $.ajax({ ...

I need assistance with displaying this array either using CSS styling or in table format. Can someone please

Seeking assistance to display this data in table format using tables, css, or bootstrap. Can anyone help? Utilized PHP Code: $url = "http://some-website.com/api/message"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = cur ...

New jQuery div elements do not have animations when using $(document).on

After creating an animation function that worked well with hovering over squares and leaving a trail, I later needed to add or remove squares based on the page size. Seeking help from here, I discovered my bind animation function wouldn't work on new ...

I am having trouble viewing elements located below a div that I created using CSS

Currently, I am working on creating a portfolio page where the initial page opens with a height of '100vh' and width of '100%', which seems to be functioning correctly. However, I am facing an issue where I am unable to scroll down to v ...

Using jQuery: What is the best way to implement form validation in jQuery before submitting?

I've created a Bootstrap form as shown below: <form id="loginForm" method="post" action="" role="form" class="ajax"> <div class="form-group"> <label for="userName"></label> <input type="text" class="form-co ...

Discovering every row in a table's <tbody> section through the power of jQuery

I am struggling to fetch all rows from a <tbody> segment in a table, and I need guidance on the correct syntax. Below is a sample snippet of the table structure along with my latest attempt using jQuery! Table segment: <tbody> <tr> & ...

Tips for positioning a div element within the body of a webpage to maintain a predetermined height and width

Currently, I am developing a single-page application using AngularJS. I have specific routes in mind where I want to introduce new HTML templates. To accomplish this, I have created a container labeled with the ID #main positioned between two navbars (he ...

Enhance the functionality of jQueryUI sortable by enabling scrolling and incorporating a delay specifically for mobile

I have an inline thumbnail gallery that can be sorted using jQueryUI and the touch punch plugin for mobile devices. Everything is functioning correctly, except on mobile devices with a large number of images. I am unable to scroll down the screen to view ...

Arranging particular placement

How can I position my links to the right of the second photo (opus1) and slightly below the first photo (coverPhoto)? I've tried various methods but haven't found a simple solution yet. <!DOCTYPE html> <html lang="en"> <head ...