Tips for activating the button in Angular.js only when every checkbox is selected within a dynamically loaded display

In my AngularJS view, I have multiple checkboxes representing different terms of an agreement.

<div class="modal-body">
<div class="col-xs-12 form-group">
    <p>I acknowledge that I understand the following:</p>
    <div class="checkbox" >
        <label>
          <input type="checkbox" ng-model="agreement.term1">Some term1 goes here
        </label>
    </div>
    <div class="checkbox" >
        <label>
          <input type="checkbox" ng-model="agreement.term2"> Some term2 goes here 
          is not a credit transaction.
        </label>
    </div>
    <div class="checkbox" >
        <label>
          <input type="checkbox" ng-model="agreement.term3"> Some term3 goes here
        </label>
    </div>
    <div class="checkbox" >
        <label>
          <input type="checkbox" ng-model="agreement.term4"> Some term4 goes here
        </label>
    </div>
    <div class="checkbox">
        <label>
          <input type="checkbox" ng-model="agreement.term5"> Some term5 goes here
        </label>
    </div>
    <div class="checkbox" >
        <label>
          <input type="checkbox" ng-model="agreement.term6"> Some term6 goes here
        </label>
    </div>

</div>

Below the checkboxes, there is a button that should only be enabled when all checkboxes are checked. This markup is displayed in a dynamically loaded modal window. I attempted to implement this functionality based on a solution from this article, but it resulted in errors when the view was loaded dynamically.

<div class="modal-footer">
<button type="button" class="btn btn-primary pull-left" data-dismiss="modal" ng-click="$close();">Confirm</button></div>

Answer №1

To implement this functionality, you can follow these steps:

<button type="button" class="btn btn-primary pull-left" data-dismiss="modal" ng-click="$close();" ng-if="agreement.term1 && agreement.term2 && agreement.term3 && agreement.term4 && agreement.term5 && agreement.term6">Confirm</button>

By using ng-if, the button will only be visible when all 6 conditions are met.

Alternatively, you can utilize the checklist-model add-on. By placing all checkboxes in a single array model and checking its length, you can simplify the process:

Assign each checkbox to the model:

<div class="checkbox" >
        <label>
          <input type="checkbox" checklist-model="agreement" checklist-value="true"> Some term1 goes here
        </label>
    </div>

// Repeat for all checkboxes



<button type="button" class="btn btn-primary pull-left" data-dismiss="modal" ng-click="$close();" ng-if="agreement.length === 6">Confirm</button>

As suggested by Paul, you can also consider using

ng-disabled="agreement.length < 6"
instead of ng-if if you prefer disabling the button rather than hiding it.

Answer №2

To enhance your functionality, integrate the code snippet below into your existing scope:

$scope.verifySubmission = function() {
   var allAccepted = $scope.acknowledge.agree1 && $scope.acknowledge.agree2 &&
                    $scope.acknowledge.agree3 && $scope.acknowledge.agree4 &&
                    $scope.acknowledge.agree5 && $scope.acknowledge.agree6;
   return !allAccepted;
}

Subsequently, apply the following to your button element:

<button type="submit" ... ng-disabled="verifySubmission()">Submit</button>

This configuration presumes you prefer the button to remain visible yet disabled instead of being hidden.

Answer №3

After experimenting with various methods, I found a solution that worked well for me. Instead of relying on a watcher, I decided to trigger a function on click within the vicinity of the checkbox.

The function looks like this:

$scope.agreement.doIfChecked = doIfChecked; 
function doIfChecked(){
    $scope.agreement.isAnyUnchecked = !($scope.agreement.term1 && $scope.agreement.term2 && $scope.agreement.term3 && $scope.agreement.term4 && $scope.agreement.term5 && $scope.agreement.term6);  
}

To implement this, I used the ng-click directive on the parent div surrounding the input box as shown below:

<div class="checkbox" ng-click='agreement.doIfChecked();'>
        <label>
          <input type="checkbox" ng-model="agreement.term1"> Some term1 goes here.
        </label>
    </div>
    <div class="checkbox" ng-click='agreement.doIfChecked();'>
        <label>
          <input type="checkbox" ng-model="agreement.term2"> Some term2 goes here.
        </label>
    </div>

Furthermore, the button markup is structured as follows, utilizing the ng-disabled directive appropriately:

<div class="modal-footer">
<button type="button" class="btn btn-primary pull-left" data-dismiss="modal" ng-disabled="agreement.isAnyUnchecked" ng-click=" $close();">Confirm</button>

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

I'm currently in the process of building a brand new React.js application by utilizing the command "npx create-react-app". However, upon completion, I encounter an error while attempting to run it with the command "npm start"

Below is the complete error message Error message from ./src/index.js at line 1, character 58 Failed to parse module: Unexpected token found at (1:58) The following loaders were used to process the file: * ./node_modules/@pmmmwh/react-refresh-webpack-pl ...

Storing information in a database via a web API using AngularJS

I am new to the world of AngularJS and Web API. I am currently facing challenges when trying to send data to a database through Web API and Angular. I have successfully managed to load JSON data from the Web API. Any assistance on this matter would be grea ...

Prevent unauthorized manipulation of specific objects' hrefs in Javascript / Identify the source of href modifications

I am currently dealing with an outdated application that populates a div by initiating a JavaScript function on a remote server and then injecting the resulting content into the DOM. Interestingly, when using Firefox, the application unexpectedly alters a ...

How to determine the completion of async iteration in HTML using the lightswitch concept

I am facing a challenge with nested async operations. I am unsure how to determine when everything is finished. Below is a snippet of my code: msls.showProgress(msls.promiseOperation(function (operation) { screen.Staff.getConfirmedWaiters().then( ...

Is there a way to alter the camera's focal point in THREEJS to focus on a specific object already in the scene?

Currently, I am working with a scene extracted from a THREEJS example (https://threejs.org/examples/webgl_loader_fbx.html) that involves importing and displaying a threejs.json scene. The rendering of the scene works perfectly; I have a grid in place with ...

The media query for mobile is not functioning properly

Hello, I have a page with MVC that includes CSS for iPad and mobile devices. The media query for iPad (@media only screen and (min-device-width: 768px) and (max-device-width: 1024px)) works perfectly, but I am struggling to get the media query for mobile ...

adjusting dimensions of swf file within html code

I am trying to insert a small speaker icon from the following url: . However, when the flash is loaded, the dimensions of the swf file exceed those specified in the html tag. How can this issue be resolved? <div style="display: inline;float:center;"& ...

Arranging group collection post query using aggregation operations

When I use the aggregate function to get all users with the same name, the results returned by the API are not sorted even though I specify to sort by the 'name' field. const aggregateQuery: any = [ { $group: { _id: '$name', ...

Issue with file path reaching into the public directory

Here is the image of the folder path:- https://i.sstatic.net/GESWX.png The CSS file seems to not be working in the exercises folder. It is only importing the header-ejs file without any styles, but it works fine for chapters and other files. How can I re ...

Is there a way to make sure all source code is aligned to the left?

I'm interested in enhancing the appearance of the source code on my WordPress website. Currently, it looks cluttered with unnecessary empty lines and alignment issues. Is there a way to optimize the source code so that everything is left-aligned and ...

What is the method for incorporating text into the Forge Viewer using three.js?

Currently, I am attempting to incorporate TextGeometry into the viewer using Three.js. I am curious about the feasibility of this task and the steps to achieve it. After exploring the documentation, I encountered challenges as the Forge viewer operates on ...

What is the best way to retrieve the full image path in a React component using the `<input>` file element and then save it to the local state?

Creating a basic shopping app using react, react-router, and bootstrap has been an exciting project for me. One of the features in my app is a form where I can add new products to the database. In this form, I can input details such as the product name, d ...

What is the best way to avoid cluttering the URL with a lengthy list of HTML code when adding a navigation bar to a website

I have created a navigation bar with base.html, styles.css, and main.js. Within the navigation bar, I have included links to about.html and contact.html using: {%block content%} ... {%endblock%} However, when I click on the "About" link in the navigatio ...

Submitting a form into a database with the help of AJAX within a looping structure

Trying to input values into the database using AJAX. The rows are generated in a loop from the database, with each row containing a button column. Clicking on the button submits the values to the database successfully. The issue is that it only inserts va ...

The navigation bar fails to respond when clicked on mobile devices

Calling all developers! I need a helping hand to tackle an issue that's holding me back from completing this website. Can someone please take a look at on their smartphone browser and figure out why the responsive menu icon isn't working when cl ...

Why don't inline style changes implemented by JavaScript function properly on mobile browsers like Chrome, Dolphin, and Android?

View the jsFiddle here Issue on PC/Windows browser: When performing action, h1 header "gif" disappears and video starts playing. Problem on mobile devices: The gif does not disappear as expected but the video still plays indicating that JavaScript is fun ...

Waiting for user submission before executing a JavaScript function

const onSubmit = ()=>{ someFunction().then(()=>{ // next step is to wait for user interaction by clicking a specific button // user initiates the action // perform specific task }) } Essentially, after the initial form submissi ...

Automatically populate date input fields

I am currently using ng-repeat to loop through an object that contains various properties, one of which is a date. My goal is to display the date that is already in the object to the user, while also allowing them to modify the date if needed. To achieve t ...

Shorten a value within an array of objects using JavaScript

How can I truncate a value in an array of objects based on specific criteria? Let's use the following example: var items = [ { name: "CN=arun, hjsdhjashdj,jsdhjsa,kshd", status: "Present" }, { name: "CN=manohar, abcdefghij,111111,2222222", s ...

Rendering elements dynamically using ng-repeat following an AJAX request

Feeling frustrated, I made an $http request ApiService.get_request_params("api/endpoint").then( function(data) { $scope.customers = data }, function(err) { } ); The $scope.customers should be bound to an ng-repeat. I can see the ...