Is it possible to append the .active class to a div upon clicking a button?

I'm using bootstrap's tab system to create a "set up your account" page. The steps are displayed at the top of the page above the tab-panes. When a user clicks on an option in step one, it loads the step two tab and adds the active class to the corresponding steps div. Although I have this functionality working, I am struggling to highlight the current step the user is on. For example, when the user is on step one and clicks "save and continue," the next tab/step loads but the appropriate div for that step is not highlighted as active.

I would like the steps to hand off the "active" class, similar to how the bootstrap default tabs function, where the current step is highlighted. However, the "steps" divs are not part of the tab system but need to be affected by it.

Thank you!

https://i.sstatic.net/cFke6.png

Answer №1

My goal is to trigger the loading of the step two tab and activate the appropriate steps div when an option in step one is clicked.

Since you haven't provided any markup, I'm assuming you're unsure of where to begin. You can refer to the Bootstrap documentation to put all this together.

Start by setting up the tab markup with the data-toggle="tab" attribute.

<div class="text-center"> 
 <a class="btn btn-primary" href="#step1" data-toggle="tab">Step 1</a>
 <a class="btn btn-default" href="#step2" data-toggle="tab">Step 2</a>
 <a class="btn btn-default" href="#step3" data-toggle="tab">Step 3</a>
</div>

Then, set up your tab-content with individual panes containing the buttons.

<div class="tab-content">
  <div id="step1" class="tab-pane active">
     <div>
        <h1>Step 1</h1>
     </div>
     <div class="text-right"> 
         <a class="btn btn-default next" href="#">Next</a>
     </div>
  </div>
  ...
</div>

Utilize the Bootstrap example for event handling, shown.bs.tab. This event fires on tab show after a tab has been shown.

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  e.target // newly activated tab
  e.relatedTarget // previous active tab
})

For instance, in step one, which involves completing your basic profile, clicking "save and continue" at the bottom loads the next tab/step and activates the appropriate div for that step.

Create a click event to handle the button click and display the next pane.

$('.next').click(function () {
    var nextId = $(this).parents('.tab-pane').next().attr("id");
    $('[href=#' + nextId + ']').tab('show');
});

Check out this fiddle where everything is integrated for your review: http://jsfiddle.net/kmx4zx6n/

Answer №2

Learn how to use Jquery's addClass and removeClass functions

JQUERY

$('.main-class a').on('click',function(){
  $('a').removeClass('active');
  $(this).addClass('active');
});

HTML

<div class="main-class text-center"> 
 <a class="btn btn-default active" href="#step1" data-toggle="tab">Step 1</a>
 <a class="btn btn-default" href="#step2" data-toggle="tab">Step 2</a>
 <a class="btn btn-default" href="#step3" data-toggle="tab">Step 3</a>
</div>

CSS

.active{
    background-color:green !important;
  }

Check out the live DEMO here

Answer №3

To style in CSS, you can try the following:

.section div { background-color: green; }
.section.active div { background-color: yellow; }

Does this meet your needs?

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

How to display percentage value on ReactJS Material UI progress bar

For displaying the progress completed in numbers, I utilize the Linear Determinate component. Take a look at the image below to see how it appears. ...

Adjusting padding evenly across an unspecified number of columns

I'm currently facing a challenge with a multi-column layout. In a basic setup of three columns, I aim to adjust the padding so that each gap between the columns is consistent (2rem). However, the tricky part lies in crafting rules that can be applied ...

What is the best way to create a border for the active class nav-item in Bootstrap 4?

I am facing an issue with changing styles in the nav-item. I have created a separate style sheet and added the necessary code to make the changes. Surprisingly, the changes are reflecting well in Internet Explorer but not in Firefox. Can anyone guide me on ...

Unable to manipulate Ajax response in Firefox extension

I am in the process of creating an AJAX call that will retrieve a complete HTML page and then extract the necessary values from the response. Despite trying various solutions, I am facing an issue where the code works fine when integrated into another HTML ...

Create a responsive layout with divs using Bootstrap

As an aspiring Bootstrap and Ruby on Rails developer, I am in need of an adaptable "div" element. Here is my current code snippet: <div class="container-fluid"> <div class="row-fluid"> [if true] <div class="span2"> div1 ...

Transform ternary conditional operators into if-else statements

I encountered an issue while attempting to convert a simple minified jQuery code to an if else statement. The error message "Can't find variable: i" is displayed. Check out the original code below: var c = slider.activeIndex; 0 === c ? (slider.slid ...

ng-html-bind for a label with a checkbox

Currently, I have a view that uses the standard bootstrap layout for checkboxes. In this layout, the input is located within the label tag. <div class="checkbox"> <label ng-bind-html="vm.trustedHtml"> <input type="checkbox" ng- ...

Guide on displaying a modal from a separate file onto my HTML page with Laravel and Ajax

In my project, I am utilizing Laravel and AJAX. Inside the index.blade.php file, I have the following code: <a onclick="addForm()" class="btn btn-success " style="float:right;">Tambah</a> Additionally, I have the following script: <script ...

Struggling to extract information from the front-end by utilizing Node.js/Express with body-parser and encountering difficulties

Attempting to transfer data from the frontend to a node server utilizing body-parser Shown below is the frontend code: <body> <h1>Donate!</h1> <h2>Select A tier</h2> <label for="donate-tier-select">Suggested Sp ...

The modal window obstructs my view on the screen

You are working with a modal form that triggers a POST method in your controller https://i.sstatic.net/0vbOy.png Here is the view code: <div class="modal fade" id="agregarProducto"> <div class="modal-dialog" role="document"> < ...

AngularJS does not support the use of $(this) syntax

I have encountered an issue while developing a Chrome extension using AngularJS. I would like to add buttons to my popup page, and I want the ancestor node to disappear when a button is clicked. Here is the code snippet: in popup.html <div class="dea ...

Master the Art of Animating Letters in the DOM by Navigating Through Any Array of Characters

I am attempting to implement a typewriter effect animation where a new message is displayed and animated as I type into an input box. Initially, I tried using a global char variable to iterate through each element of the array. However, whenever I passed ...

An error occurred while trying to insert the data

As a beginner in PHP, I recently attempted to work on a project that involved using AJAX with PHP. The issue I encountered was related to a specific div ID called 'forms2', which contained a form tagged with the class 'create'. Upon cli ...

Removing quotes from specific JSON values using JavaScript/jQuery

Currently, I am receiving the following data from an ajax call: "nodes": [ {"ID":"87","score":"-0.2","dscore":"-0.4","x":"250","y":"250","name":"TEST","ticker":"TEST","datafrom":"0000-00-00","r":"28","fixed":"true","color":"grey"} ...

Is the CSS :not selector failing to operate as expected?

I have been trying to use the :not selector in my code, but for some reason it's not working. I can't figure out why this is happening, especially since the ul element does have the webmedia-gallery class. Any thoughts on what could be causing th ...

After reloading the data tables, analyze the information and dynamically highlight any rows in red that remain unchanged

I have a table that is refreshed every 5 minutes using ajax.reload(). One of the rows in this table is labeled as CIP. My goal is to highlight the CIP row on each refresh where the value remains unchanged from the previous value (value received in the la ...

Equal-height rows and columns using Flexbox

I've been working on a row layout with two columns, each set to 50% width. The left column contains an image, while the right column displays text. Here is my HTML: .row{ display:flex; ...

Creating a Design That Works Well on Both Desktop and Mobile Devices

I have created this design using CSS and Bootstrap. However, I am encountering an issue with responsiveness. Can someone assist me in making this design responsive? HTML: .ordering .spanone { position: absolute; width: 100px; left: 43%; right: ...

The height of the input element is greater than the size of the font

I am facing an issue with an input element that has a height 2px higher than what I expected. Here is the relevant CSS: .input { font-size: 16px; padding: 15px; border: 1px solid black; border-radius: 3px; width: 400px; } <input class=" ...

Linking CSS3D objects in a three.js environment

In my current project, I am working on creating a navbar within the three.js environment. I have set up all the necessary CSS3D configurations and added a mouse down event listener. However, I am facing an issue with the intersects not displaying anything. ...