Using JQuery to dynamically swap out the Bootstrap progress bar

I created a progress bar using bootstrap with a default value of 33% and a switch that toggles between all active or inactive. When the switch is activated, I want the progress bar to show a value of 100%, and when it's inactive, a value of 0%.

However, this only works correctly the first time I click the switch. If I click the switch multiple times, it doesn't function as expected:

To view the issue in action, visit: http://jsfiddle.net/RNMJ7/1/

The main structure of the progress bar is outlined below:

<div class="row progbar">
    <div class="col-md-2 col-xs-2"><p class="progressinfo">33%</p></div>
                    <div class="col-md-9">
                        <div class="progress"> 
                            <div class="progress-bar progress-bar-success" style="width: 33%">
                                <span class="sr-only">33% Complete (success)</span>
                            </div>
                        </div>      </div></div>

The JQuery code for the progress bar changes (triggered by the switch's onClick event) is displayed below:

$(".switch").click(function () {
    $("#tp1")[0].innerHTML = ( $("#tp1").text() == "Activar todas" )? 'Desactivar todas' : 'Activar todas';
    $(".progressinfo")[0].innerHTML = ( $(".progressinfo").text() == "33%" )? '100%' : '0%';
    var varVisible2 = ( $(".progressinfo").text() == "33%" )? $('.progress-bar-success').css( "width", "33%" ) : $('.progress-bar-success').css( "width", "100%" );
    var varVisible3 = ( $(".progressinfo").text() == "0%" )? $('.progress-bar-success').css( "width", "0%" ) : $('.progress-bar-success').css( "width", "100%" );

$('div.panel').each( function(){
    this.className = "panel " + varVisible;
});

var miniswitch = ( $("#tp1").text() == "Activar todas" )? false : true;

$('.miniswitch').each( function(){
    $("input[type=checkbox]").attr('checked',miniswitch);
});
});

Answer №1

It is important to include the following code:

|| $(".status").text() == "Complete"

in your script, just like this:

$(".status")[0].innerHTML = ( $(".status").text() == "In Progress"
|| $(".status").text() == "Complete" )? 'Completed' : 'In Progress';

Check out the demonstration here: JSFiddle

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

Surprising 'T_ENCAPSED_AND_WHITESPACE' error caught me off guard

Error: An error was encountered while parsing the code: syntax error, unexpected character (T_ENCAPSED_AND_WHITESPACE), expected identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\html\updatedtimel ...

Attempting to showcase PHP print statement within HTML using JSON

I want to showcase two PHP echo messages from a different PHP file on my HTML body page. I aim for the echo message to appear when I click the submit button, without being redirected to the PHP page. To achieve this, I need to establish a connection betwe ...

How come my container-fluid div isn't encompassing the box divs too?

I am currently in the process of creating a dice using the bootstrap grid system. Although I have not completed it yet, here is a small snippet of the code that showcases my progress. However, when I inspect my webpage using developer tools (see attached i ...

Emphasize an element when hovering over it

I am looking to create a custom hover effect for highlighting elements in a Chrome extension I'm developing. The goal is to achieve a similar behavior to using the magnifying glass tool in Chrome Dev Tools, where only the hovered element is highlighte ...

Transform the date into the specified ISO format of YYYY-MM-DDThh:mm:ss.sTZD

I am looking to transform a date into the ISO format YYYY-MM-DDThh:mm:ss.sTZD using JavaScript. Currently, I am able to convert the current date string into the format yyyy-MM-dd'T'HH:mm:ss.SSSZ. For example: 2016-01-11T02:40:33.117Z. However, I ...

Using jQuery to pause execution until a function returns before proceeding

After updating to version 1.10.2, I encountered some issues with jQuery. Despite my attempts to find a solution, none of the methods I've tried have seemed to work. It's possible that I am missing something in my approach. Any assistance would b ...

Is there a way to make my for loop search for the specific element id that I have clicked on?

When trying to display specific information from just one array, I'm facing an issue where the for loop is also iterating through other arrays and displaying their names. Is there a way to only show data from the intended array? const link = document. ...

What is the best way to add spaces between each element in an array of arrays that are nested within a map

Hello there! I'm seeking guidance on correctly formatting a Multi-Array (an array of arrays) that is nested within a map array. To simplify, I have the following code snippet: const [gradosData, setGradosData] = useState([]) const agregarComa = (grado ...

The design and structure of the CSS link, as well as

What motivates most developers to utilize the following links in their code: <link href="/js/jquery-ui/css/flick/jquery-ui-1.8.23.custom.css" type="text/css" /> <link href="/css/main.css" type="text/css" /> <link href="/css/table.css ...

Firebase Authentication: Invoking `createUserWithEmailAndPassword` triggers `createAuthUri` in case of an error

Whenever a user attempts to create an account with an existing email, calling firebase.auth().createUserWithEmailAndPassword(email, password) results in the error message "QUOTA_EXCEEDED : Exceeded quota for email lookup.". Below is the code sni ...

I am unable to load any HTML files in my VueJS iframe, except for the index.html located in the public

Within the template of my vue component, I am utilizing the following code: <iframe width="1000vw" height="600vh" src="../../public/myHtmlFile.html"></iframe> Unfortunately, the file specified in the src attribut ...

Vue app: ESLint throwing error for undefined global variable in component during npm run serve

In my main.js file, I am initializing a new Vue instance on the window object: window.todoEventBus = new Vue() Within my components, I am attempting to access this global todoEventBus object like so: created() { todoEventBus.$on('pluralise&apos ...

Connect a group of <div> elements via a hyperlink

Every time I attempt to add a hyperlink to the code snippet below, it interferes with the layout of the image and only links the icon rather than the entire flex container. I've experimented with <span> elements but haven't found a successf ...

"Attempting to access a variable defined in one pipe results in it being undefined in a

Currently, I am utilizing gulp to process pages for a website. Some of these pages are PHP files, however, after going through the template engine, they all end up with a .html file extension. My intention is to add a property to each file indicating if it ...

Quantifying website loading times using JavaScript code

Looking for a solution to load a webpage, analyze page load time and network call statistics using tools like Selenium. I'm interested in obtaining similar information as Chrome DevTools Network tab, including details on request status, type, size, an ...

The 'ui.bootstrap' module is currently unavailable in AngularJS

Currently, I am encountering a peculiar issue with my angularjs project. On my website - www.server.com/pwm (home page), there is an anchor tag that redirects me to another page - www.server.com/publishers. When I navigate from the home page to the publis ...

Displaying the value of a jquery variable in an HTML document

I'm tackling a problem differently today compared to yesterday, but my knowledge of jQuery and JavaScript is quite basic. My goal is to increment the transform value of a div every 5 seconds: <div style="transform: translateX(0px);" id="slide_ima ...

Stop the execution of the JavaScript function when clicked

Greetings, as a newcomer to the realm of JavaScript and AJAX, I am seeking assistance with a specific function on this page. The concept revolves around triggering different JavaScript functions based on user interaction with an image map. Initially, when ...

Ensure that the "position: absolute" is anchored to the document rather than the parent container

How can I add a div to any section of the webpage and have it positioned absolutely in relation to the entire document, rather than being affected by a parent element with relative positioning? ...

The Bootstrap navbar toggle is unresponsive and will not expand

I want to implement a Bootstrap navbar to expand the webpage when it is opened on a mobile phone. Below is the code snippet of how I have used Bootstrap in my HTML file: <html> <head> <link rel="stylesheet" href="https://cdn.jsdeliv ...