Adapting the size of four columns using Jquery

I've managed to create a functioning jsfiddle example with the following JavaScript code:

$(".blah").on('click', function () {
    $(this).removeClass('col-md-9');
    $(this).toggleClass('col-md-3 col-md-9');   
    $('.blah').not(this).toggleClass('col-md-3 col-md-1');
});

This script allows me to achieve my goal of using Bootstrap to create 4 columns that always add up to a total width of "12". Currently, when I click on a column, it expands while the others shrink accordingly. However, I'm facing an issue where if one column is already set to col-md-9, clicking on another column should enlarge it and shrink the rest. On my test page, this functionality works perfectly by displaying the columns side by side. Despite not understanding why this isn't replicated in the jsfiddle example, the concept remains clear. My intention is for the clicked column to always be larger than the others, unless I click on the already expanded column, which should then reset all columns to equal sizes.

Answer №1

$(".click-me").on('click', function () {
    $('.click-me').removeClass('col-md-8').addClass('col-md-2');
    $(this).addClass('col-md-8'); 
});

If you want to try it out, here is the updated jsfiddle link

Let me know if you have any questions!

Answer №2

Using jQuery

$(".click-me").on('click', function () {
    if ($(this).hasClass('big')) {//if big
        $(this).removeClass('big').addClass('small')//remove big add small
        .siblings('.box').removeClass('size1 size9').addClass('size3');//remove 1,9 add 3
    } else {//if not big
        $(this).removeClass('size1 size3').addClass('big')//remove 1,3 add big
        .siblings('.box').removeClass('size3 size9').addClass('size1');//remove 3,9 add 1
    }
});

.toggleClass() can be tricky, ensure you use .removeClass() when a class should never be present and use addClass() to always include a class.

I included an if statement since multiple classes are being moved around, and the clicked element could have any of those classes initially.

I also added a class of .box to apply float:left for inline nesting.

Check Out My Fiddle...

This can also work without a grid system: view example here...

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

Creating an SQL select statement with a three-level expression is a complex task that requires careful planning and

How can I create a SQL select query with 3 levels of expressions and statements? Typically, my website operates on an SQLite database and the search results are displayed using the following SQL query: "SELECT DISTINCT * FROM amz WHERE Title LIKE \" ...

Positioning elements precisely on a webpage through absolute positioning and floating them

This seems to present a conflicting situation. Ideally, I am looking to position two child elements on the left and right edges of the parent element, vertically centered, and overlaying all other sibling elements. ...

Exploring the World of Html

I'm struggling with an HTML problem related to a web programming class I'm taking. The assignment involves creating a video game using HTML and JavaScript, where an image moves randomly on the screen and the player must click on it as many times ...

Expanding the padding of the selected item to create more breathing room

Upon examining the following: https://jsfiddle.net/h8kmove5/27/ Let's say you select 9 at the bottom. The display shows 8 9 10. My intention is to create some additional space on either side of 9, or any other active item for that matter. This way, ...

Enhance the input by incorporating more fields without altering the existing content

Currently, I am working on creating an input form that includes multiple fields and has the capability to generate a preview of the entered content in a separate div. One key feature I would like to implement is allowing users to add additional fields as n ...

The image is not resizing correctly

Currently, the background-image on my website displays properly when the browser occupies the entire screen. However, when I resize the browser window, the image shrinks to a certain point and then suddenly stops adjusting. Here is a visual representation ...

Using XSL variables in JavaScript code

I've noticed that there have been similar questions asked, but none of the solutions seem to help in my case. So, I have this variable named 'var': <xsl:variable name="var"> val </xsl:variable> Now, I want to use it like thi ...

React not displaying images with relative paths

In the past, I used to import images in React like this: import person from '../images/image1.png' And then use them in my code like this: <img src={person} alt="" /> Now, for some reason, I want to directly specify the image pa ...

How can I eliminate the blending effect between different layers?

Experimenting with blend modes has led me to create a composite image featuring a shirt and a pocket. Initially, everything seemed fine when I only had the shirtVer and shirtHor layers. I could easily merge them and adjust the colors of the checkered patte ...

Keep the webpage current without the need to refresh it with a button click

I've been pondering if using .ready would be the right approach to continuously update a webpage. Imagine a scenario where I have a page that tracks the number of a specific tag and displays it on the webpage. The count can be modified with basic Jav ...

The top-center alignment in Toaster (ngx-toastr) is missing a top margin

I recently started using the ngx-toastr library in my project. I have a message service that displays error messages at the top-center of the screen with the following code: @Injectable() export class MessageService { constructor(private toastrServic ...

Design a logo to serve as the main button on the navigation bar of the

I've been experimenting with adding a logo instead of the 'Home' text in my navigation bar, but I'm not sure where to implement it. Should I add it within #nav or separately? Also, I want the nav bar to stay fixed without overlapping on ...

The module for the class could not be identified during the ng build process when using the --

Encountering an error when running: ng build --prod However, ng build works without any issues. Despite searching for solutions on Stack Overflow, none of them resolved the problem. Error: ng build --prod Cannot determine the module for class X! ...

Utilize jQuery and $.get to send data to a php page

I am facing an issue with my datepicker form using jQuery UI. I need to select a date and then send that selected date to a PHP file. Here is the code I have attempted, but it seems to not work: $( "#datepicker" ).datepicker({ onSelect: function(dateT ...

Implementing ajax calls to interact with every model object in DJANGO

I am currently facing an issue with implementing AJAX in my project. While the functionality is working fine, I am encountering a problem when trying to apply AJAX to each instance of a model. It seems that it's only being applied to the top object an ...

What are the steps to provide code so that others can easily incorporate your content into their HTML pages?

Is the solution to this problem simpler than I realize? In my Zend Framework PHP web application, I plan to create a straightforward API that generates a user account report. The report's content will be basic, like so: <ul> <li>Item ...

Problem encountered with Blueimp gallery and Twitter Bootstrap

Blueimp gallery is being used to showcase a set of 8 images on a website, divided into two rows. The 5th thumbnail (first image on the second row) appears broken, even though it can be seen in the carousel presentation. Here's the link to view the th ...

The result of combining two JavaScript variables

When I multiply a variable by 2 and assign the value to another variable, why do I get 0? My goal is to toggle the visibility of blocks every time a button is pressed. Oddly enough, it works when I use count++ * 2. For code reference, please refer below: ...

One cannot loop the .click function in order to create multiple buttons

I am currently in the process of creating multiple buttons, each with its own unique function. While everything is functioning perfectly when outside of the loop, I am encountering an issue once the click function is inserted within the each loop. $.each( ...

What is the best way to update only the fields in a user's profile that they have filled out in the form?

I am having an issue updating user profile details using my form, as it only works when all input fields are filled. How can I update only the profile details that are filled in the form? The form allows for updating 4 fields: Name, Email, Age, and Passwo ...