The functionality of the checkbox button is not functioning properly

I have implemented a set of checkboxes styled as Bootstrap buttons in order to give them a different look from the standard checkboxes. Below is the code snippet:

    <div class="btn-group" data-toggle="buttons">

        <label class="btn btn-primary" id="area_selection_checkbox">
            <input type="checkbox" ng-model="study_area.a" ng-true-value="'design'" autocomplete="off"/> Design
        </label>

        <label class="btn btn-primary" id="area_selection_checkbox">
            <input type="checkbox" ng-model="study_area.b" ng-true-value="'economics'" autocomplete="off"> Economics
        </label>

        <label class="btn btn-primary" id="area_selection_checkbox">
            <input type="checkbox" ng-model="study_area.c"  ng-true-value="'management'" autocomplete="off"> Management
        </label>

        <label class="btn btn-primary" id="area_selection_checkbox">
            <input type="checkbox" ng-model="study_area.d" ng-true-value="'marketing'" autocomplete="off"> Marketing
        </label>

        <label class="btn btn-primary" id="area_selection_checkbox">
            <input type="checkbox" ng-model="study_area.e" ng-true-value="'software engineering'" autocomplete="off"> Software engineering
        </label>

        <label>
            <input type="checkbox" ng-model="softskill_filter.e" ng-true-value="'creativity'"/>
            Creativity
        </label>

<tt>value1 = {{study_area}}</tt><br/>
    </div>

It's worth noting that the only checkbox without a class or ID is the one that actually functions properly. And here is the CSS for the buttons:

#area_selection_checkbox {
  border-radius: 0;
  width: 200px;
  margin: 1px;
  display: block;
  cursor: pointer;
  background-color: #EDF1F5;
  border-color: #EDF1F5;
  color: #787985;
  font-weight: 500;
  outline: none !important;
  padding: 10px 12px;
  border: none;
}


#area_selection_checkbox.active, #area_selection_checkbox:active,
  #area_selection_checkbox.active:hover, #area_selection_checkbox:active:hover{
    background-color: #787985;
    border-color: #787985;
    color: #FFFFFF;
}

#area_selection_checkbox:hover, #area_selection_checkbox:focus {
  background-color: #E1E6ED;
  border-color: #E1E6ED;
  color: #787985;
}

However, I am facing an issue where clicking on the buttons does not update the ng-model, almost like the checkbox functionality isn't recognized at all. Even though the code for the ng-model is present on the page, it appears to have no effect:

 <tt>value1 = {{study_area}}</tt><br/>

Could there be something missing in my implementation? Just so you know, I am utilizing Bootstrap 3.3.7. Thank you.

Answer №1

If you're working with angular directives, it's important to ensure that you have angular.js loaded. Disabling autocomplete on checkboxes is unnecessary - you can simply omit that. Take a look at ng-repeat as well, as it can help eliminate duplicate code in your template.

Make sure to define your choices within your angular controller:

this.currentChoice = 'a';
this.availableChoices = [{
        id: 'choice-a',
        label: 'Choice A',
        value: 'a'
    }, {
        id: 'choice-b',
        label: 'Choice B',
        value: 'b'
    }
];

Then, in your template, implement something like the following:

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary" id="{{choice.id}}" ng-repeat="choice in ctrl.availableChoices">
         <input type="checkbox" ng-model="ctrl.currentChoice" ng-true-value="'{{choice.value}}'"/> {{choice.label}}
    </label>
</div>

This advice is provided without testing and may vary depending on the version of Angular you're using. Keep in mind that Angular syntax can evolve over time, but this should give you an idea of how to enhance your code.

Answer №2

I've managed to create a functional fiddle that seems to be working smoothly. The key difference is the inclusion of angular-ui-bootstrap library.

var app = angular.module('myapp', ['ui.bootstrap']);

This implementation closely resembles what you were attempting to achieve.

Would this be a step in the right direction for you?

Access the fiddle 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

How can I incorporate popups in React using mapbox-gl?

Currently utilizing mapbox-gl within React, everything seems to be functioning properly except for the integration of mapbox-gl's Popups. I have the function let Popup, but I am uncertain about how to incorporate it. renderMap() { if (this.props. ...

The node.js express app.get and app.post are currently malfunctioning

I have encountered an issue with my Express JS code. Currently, I am using app.use and everything works perfectly. However, when I try to use app.post or app.get instead of app.use, the code stops working and my IDE (WebStorm) does not recognize these meth ...

Choosing between Seneca and Hydra for developing a microservice architecture in Node.js is a decision

In my NodeJs application with microservice architecture, I'm looking for recommendations on the best framework to use. Any suggestions? ...

Is it possible to replicate Svelte's deferred transitions in Vue.js?

Is there a feature in Vue.js that is comparable to Svelte's in:receive and out:receive? I came across this functionality in the Svelte Tutorial, where it smoothly moves a todo from one list to another. You can check out how it works here: . I've ...

Determine the specific cell involved in an HTML5 drag-and-drop interaction within a table

I've been experimenting with the HTML5 drag and drop functionality in an Angular project. Here's the setup I'm working with: A container containing draggable 'objects' A table where users can drop the dragged elements Following ...

Make sure to clear the localStorage when the tab or browser is closed, but do not

Is there a way to detect when a tab is closed in order to clear the localStorage for data sharing between tabs? The window.onbeforeunload event works, but it has two issues: It triggers on page refreshes, which is not desired. I do not want a confirmation ...

Incorporating animation effects in ajax technology

I've coded a feature that utilizes the .getJSON API from jQuery to retrieve images from the Flickr public API. You can view it on this link. Each image slides after every request is completed. Now, I would like the initial set of images to slide downw ...

accept various query parameters in a GET request in a REST API written in Node.js

Is there a way to dynamically receive multiple parameters in a GET request without knowing which ones will come and which ones won't, in order to filter using the find() method? This pertains to NODE JavaScript with Mongoose app.get('/venta&apos ...

javascript callbacks used for language translation application

I am currently working on developing a translation engine with node.js. Coming from a Python/R background, I am having trouble grasping the concept of callbacks... The sentence for translation is: var sentence = "I want to translate this" Upon clicking ...

Converting non-null values in a JSON object to null within an MVC Controller method

Take a look at the image below... The information displayed in the upper section shows the JSON data from my client that is being sent to the MVC Controller. I directly copied this from Chrome Dev Tools. In the lower part, you can see the actual data rec ...

Unlocking the secrets to retrieving authentic input attributes

Using jQuery as a connector to access and search for original DOM elements by ID is a common practice. The jQuery function get() can be used to achieve this functionality. How does this process differ when working with an input element? <html> <h ...

Creating a tree control using JSON and jQuery: A step-by-step guide

I am currently working on creating a dynamic tree view on my website using jQuery and JSON data. Each node in the tree comes with an associated checkbox, and I want to implement a functionality where clicking on a parent node automatically selects all its ...

Utilizing jQuery to Capture Select Change Events

I am trying to automatically select a hidden option based on the value selected in a visible select. Here is my jQuery code: $(document).ready(function() { var selected_val = $('#id_foo option:selected').val(); $('#id_bar').va ...

Activate mouse event when the pointer is within X units of the bottom of the browser window

My inquiry is straightforward, although the solution may not be as simple. I have an element fixed to the bottom of the browser window. When it is not needed, I want to hide it by setting its height to 0px. Whenever the user's mouse is a certain dista ...

Resetting forms in Angular after validation

Currently, I am utilizing AngularJS for form validation and everything is functioning correctly. However, once the form is valid, I proceed to send an AJAX request and reset it using jQuery code. The issue arises when the form still displays as valid even ...

How can we incorporate tab stops using jQuery?

Is there a way to stop the tab navigation when it reaches a button, rather than skipping fields using tabindex="-1"? For example, if my focus moves from input field A to Email and then to the button labeled "sdfsdf", I want to prevent moving to the test i ...

Toggling designs with a simple click

I'm currently working on a ReactJS project and I'm trying to figure out how to change the color of a button when it's clicked. I've heard about the Ripple API, but I find it quite complex to use. Can anyone offer some advice on how I ca ...

Instructions for setting a cookie to conceal a notification bar upon the second page load

I am looking for the following scenario: When a new visitor lands on my website, a notice bar fixed to the bottom of the screen becomes visible. After clicking on a menu item to navigate to another page, a cookie is set upon the second page load. This co ...

Stagnant updates in component's styling

I have a list box style stored in the component's data. It is bound to a listbox element. One of the styles within it is position: absolute. My goal is to position the box directly below the input element. To achieve this, I am trying to dynamically m ...

Testing AngularJS applications using Protractor involves running asynchronous scripts

Currently, I am in the process of creating a set of automated tests for an AngularJS application using Protractor. Prior to this task, I had no experience with automated testing, so I'm still learning the ins and outs of how it all functions. Recentl ...