1

I have modal i.e ngx-bootstrap modal in which there are two radio buttons.

I want to get a shake effect on the selected radio button as validation in case the 'Next' button is clicked.

I have already created css class for the shake effect. I want to add this css click on clicking on the button when the it is required. I tried using ngClass but it is not working as expected. How this can be done?

HTML

<ng-template #myModal>
    <div class="modal-body">
        <div class="row">
            <div class="col-md-4 col-md-offset-3 text-center">
                <input formControlName="genderName" type="radio" id="female" value="Female" [ngClass]="{'rederror': this.cf.genderName.required}">
                <label for="female" class="female">Female</label>
            </div>
            <div class="col-md-4 col-md-pull-1 ml-4">
                <input formControlName="genderName" type="radio" id="male" value="Male" [ngClass]="{'rederror': this.cf.genderName.required}">
                <label for="male" class="male">Male</label>
            </div>
            <button type="button" (click)="submit()">Next</button>
        </div>
    </div>
</ng-template>

css file

input[type=radio].rederror {
  animation: shake 0.5s 3 linear;
  border: 1px solid red;
}

@keyframes shake {
  0% {margin-left: 0;}
  25% {margin-left: 0.5rem;}
  75% {margin-left: -0.5rem;}
  100% {margin-left: 0;}
}
1

3 Answers 3

0

You can add class in event handler of submit click. sample working code (target whichever element you want class added to instead of .col-md-4) - https://stackblitz.com/edit/angular-j94h2r

  submit(): void {
    const eles = this.formContainer.nativeElement.querySelectorAll('.col-md-4');
    eles.forEach(ele => ele.classList.add('shake'));
  }
Sign up to request clarification or add additional context in comments.

Comments

0

I think the easiest thing to do would be to add the styles for the shaking effect to the :active pseudoselector.

input:active {
   // css for shaking the radio button
}

Comments

0

You have to add a check to your ngClass statement.

As an example you can use the same check as used for a click.

e.g.:

<label *ngFor="let radiobutton of radioItems">
  <div>
  <input type="radio" name="options" (click)="model.option = radiobutton"
    [checked]="radiobutton === model.option"
    [ngClass]="{'rederror': radiobutton === model.option}">{{radiobutton}}  
  </div>
</label>
<p>

see this working Stackblitz


Edit here comes your complete example:

html:

<div class="modal-body">
    <div class="row">
        <div class="col-md-4 col-md-offset-3 text-center">
            <input formControlName="genderName" type="radio" id="female" (click)="selectedOption=female" value="female"
                [ngClass]="{'rederror': selectedOption===female && shake}" [checked]="selectedOption===female">
            <label for="female" class="female">Female</label>
        </div>
        <div class="col-md-4 col-md-pull-1 ml-4">
            <input formControlName="genderName" type="radio" id="male" (click)="selectedOption=male"
                [checked]="selectedOption===male" [ngClass]="{'rederror': selectedOption===male && shake}">
            <label for="male" class="male">Male</label>
        </div>
        <button type="button" (click)="submit()">Next</button>
    </div>
</div>

components:

import { Component,  } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  public male = "Male";
  public female = "Female";
  public selectedOption = this.female;
  public shake = false;

  public submit() {
    this.shake = true;    
    setTimeout(() => this.shake = false, 500);
  }
}

Together with the working Stackblitz!

3 Comments

I want shaking effect after clicking submit button
Updated lower example + Stackblitz!
Please have a look at stackoverflow.com/help/someone-answers too

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.