0

In my html :

<input #myRadio type="radio" name="category" id="work-time" />


<button (click)="myClick(myRadio)">save</button>

In my component:

myClick(_myRadio:HTMLElement)
{

}

I have tried to save the result in a boolean type variable, but there is no attribute isChecked or Value, so I am stuck in this.

How to check if the radio button is checked on the button click by the user?

4
  • you can still get the element like javascript as w3schools.com/jsref/prop_radio_value.asp here. But I do not think it would be the best practice. Commented Oct 26, 2017 at 7:41
  • ye, I know. I managed to do it with js, but it's now allowed to do it with js Commented Oct 26, 2017 at 7:43
  • You can pass the event inside your click and check what's in it as (click)="myClick($event)" and in your function get myClick(event: any) and print it to see what's passed Commented Oct 26, 2017 at 7:46
  • What i do is a suggestion, that does not solve the problem issued above Commented Oct 26, 2017 at 8:06

1 Answer 1

1

Try ngModel:

<input type="checkbox" name="category" [(ngModel)]="category" />
<button (click)="myClick()">save</button>

NgModel binds the checkbox value (if checked true otherwise false) to a class property category. The method called by button click myClick() can then use the property:

myClick() {
  // do stuff with this.category;
}

If you'd prefer to not import the forms module, you can use property and event bindings on the input to accomplish the same result:

<input type="checkbox" name="category" [checked]="category" (click)="category = !category" />
<button (click)="myClick()">save</button>
Sign up to request clarification or add additional context in comments.

2 Comments

if the radio is checked and user clicks save I just need to set a boolean variable to be true so I can go in my if statement block.
It sounds like the input type we're looking for is checkbox, it provides a boolean value: checked or not-checked. I'll update my answer.

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.