[Fixed] How do I reset or Uncheck the Radio button in Angular programmatically?

Issue

This is my HTML page for selecting product condition using radio button. When user check New or used button I fetch New product or Used product:

     <div>
          <h5><b>Condition</b></h5>
          <label class="container">New
            <input (click)="onCondition('New')"  type="radio"  name="radio" >
            <span class="checkmark"></span>
          </label>
          <label class="container">Used
            <input  (click)="onCondition('Used')" type="radio" name="radio" >
            <span class="checkmark"></span>
          </label>
    </div>

This is my CSS for Radio button:


/* The container */
.container {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 15px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Hide the browser's default checkbox */
.container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create a custom checkbox */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
  background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
  background-color: #2196F3;
}

/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
  display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}



.slidecontainer {
  width: 100%;
}

.slider {
  -webkit-appearance: none;
  width: 100%;
  height: 25px;
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  -webkit-transition: .2s;
  transition: opacity .2s;
}

.slider:hover {
  opacity: 1;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  background: #4CAF50;
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 25px;
  height: 25px;
  background: #4CAF50;
  cursor: pointer;
}

This is the TypeScript function where I want to reset or unchecked all the radio button when this function gets executed. I want to able to uncheck the radio button to default:

/**
   * Get current categories
   * @param categoryId 
   */
  getCurrentCategories(categoryId: number) {
    this.currentCategories = categoryId;

    
  }


Solution

Try giving an id=’radio’ atribute and then give document.getElementById(‘radio’).checked = false

Leave a Reply

(*) Required, Your email will not be published