0

I have the following array in my component:

checkBox = [
        {label: 'SSN', name:'ssn', value: '1', checked:false},
        {label: 'Last Name', name:'lastName', value: '2', checked:false},
        {label: 'Role', name:'role', value: '3', checked:false},
        {label: 'UserId', name:'userId', value: '4', checked:false},
        {label: 'Office', name:'office', value: '5', checked:false},
        {label: ' Include Subordinates', name:'subordinates', value: '6', checked:false}
    ];

I have several checkboxes in my view that look like this:

<span class="input-group-addon">
          <input type="checkbox" name="ssn" (change)="checkBox[0].checked">
        </span>

and

<span class="input-group-addon">
          <input type="checkbox" name="lastName" (change)=checkBox[1].value>
        </span>

etc....

However when I it the submit button:

<button type="submit" (click)="search(checkBox)" class="btn btn-default btn-md left-button">Search</button>

I get the output false in the component even though it one is selected and I only get one false not 6 (They're 6 checkboxes)

I was assuming I would get false, false, True, False, False, False because I need to know which checkbox is selected

  public search(e){

        for (let index = 0; index < e.length; e++){
            console.log(e[index].checked)
        }

    }

full code:

<form>
        <div class="col-lg-6 col-lg-offset-3 text-center bordered">
            <div class=" col-xs-6 ">
                <div class="row">
                    <div class="col-md-12 box-content right">
                        <div class="input-group">
        <span class="input-group-addon">
          <input type="checkbox" name="ssn" (change)="checkBox[0].checked=!checkBox[0].checked">
        </span>
                            <span class="input-group-addon">
          <label>{{checkBox[0].label}}</label>
        </span>
                            <input #ssn type="password" name="ssnText" class="form-control" placeholder=" ">
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12 box-content right">
                        <div class="input-group">
        <span class="input-group-addon">
          <input type="checkbox" name="lastName" (change)="checkBox[1].checked=!checkBox[1].checked">
        </span>
                            <span class="input-group-addon">
          <label>{{checkBox[1].label}}</label>
        </span>
                            <input type="text" #lastName name="lastNameTest" class="form-control" placeholder="">
                        </div>
                    </div>
                </div>


                <div class="row">
                    <div class="col-md-12 box-content right">
                        <div class="input-group">
        <span class="input-group-addon">
          <input type="checkbox" name="role" (change)="checkBox[3].checked=!checkBox[3].checked">
        </span>
                            <span class="input-group-addon">
          <label>{{checkBox[2].label}}</label>
        </span>
                            <input type="text" #role name="roleText" class="form-control" placeholder=" ">
                        </div>
                    </div>
                </div>
            </div>

            <div class=" text-center col-xs-6">
                <div class="row">
                    <div class="col-md-12 box-content ">
                        <div class="input-group">
        <span class="input-group-addon">
          <input type="checkbox" name="userId" (change)="checkBox[4].checked=!checkBox[4].checked">
        </span>
                            <span class="input-group-addon">
          <label>{{checkBox[3].label}}</label>
        </span>
                            <input type="text" #userId name="userIdText" class="form-control" placeholder=" ">
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12 box-content ">
                        <div class="input-group">
        <span class="input-group-addon">
          <input type="checkbox" value="{{checkBox[4].value}}">
        </span>
                            <span class="input-group-addon">
          <label for="office">{{checkBox[4].label}}</label>
        </span>
                            <input type="text" #office id="office" name="officeText" class="form-control" placeholder=" ">
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12 box-content ">
                        <div class="input-group">
        <span class="input-group-addon">
          <input type="checkbox"  #subordinates name="subText" (change)="checkBox[5].checked=!checkBox[5].checked">
        </span>
                            <span class="input-group-addon">
          <label>Include Subordinates</label>
        </span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="center-block">
                <button type="submit" (click)="search(checkBox)" class="btn btn-default btn-md left-button">Search</button>
                <button type="submit" class="btn btn-default btn-md right-button">Reset</button>
            </div>
        </div>
    </form>
2
  • 1
    You're incrementing the e, not the index Commented Feb 22, 2017 at 13:55
  • Can you create a fiddle link Commented Feb 22, 2017 at 13:55

2 Answers 2

2

Try this:

public search(e){
    for (let index = 0; index < e.length; index++){
        console.log(e[index].checked)
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That is what I posted, I have that.
@Drew1208 Nope. You're incrementing the value of e. I'm incrementing index.
1

I don't see where you would change the value for the checked values? You have to use either [(ngModel)] or do

(change)="checkbox[index].checked=!checkbox[index].checked"

If you render your checkboxes with a *ngFor:

<span class="input-group-addon" *ngFor="checkBoxElem of checkBox">
     <label>{{checkBoxElem.label}}</label>
     <input type="checkbox" name="checkBoxElem.name" [(ngModel)]="checkBoxElem.checked">
</span>

To allow only one checkbox to be selected at once (just an idea, there may be a cleaner solution):

<span class="input-group-addon" *ngFor="checkBoxElem of checkBox">
     <label>{{checkBoxElem.label}}</label>
     <input type="checkbox" name="checkBoxElem.name" [(ngModel)]="checkBoxElem.checked" (change)="uncheckOtherCheckboxes(checkBoxElem.value)">

and in your component.ts:

public uncheckOtherCheckboxes(valueToKeep:number){
   this.checkBox.forEach(checkBoxElem => {
      if(checkBoxElem.value !== valueToKeep && checkBoxElem.checked){
         checkBoxElem.checked = false;
      }
   });
}

5 Comments

Doing that just causes an error saying TypeError: Cannot read property 'checked' of undefined
This is just pseudo code, I don't know how you render your checkboxes. If you do it with a *ngFor etc. but let me update my answer
I didn't use *ngFor but this help solve the issue. Now I need to figure out how to only allow the user to select one checkbox.
I have updated my answer to provide an example for that.
With the way I have it set up I cannot use *ngFor but would love too. Only one checkbox can be selected except for subordinates that one the only one allowed to be selected with another checkbox. Than I need to know what checkbox is selected and the text field input. I updated my question to show the full code. The way I have it set up it is only sending in one checkbox not all of them so the console.log() in the search method only works for the first checkbox

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.