Angular Checkbox Value to Typescript - html

i'm trying to make a quiz where there is a card and 4 questions.
I have the 4 questions Like this
<div class="col-md-6">
<div class="option" id="Answer1">
<label class="Answer1">
<input value= "Answer1"
type="checkbox"
(change) ="CheckAnswer(value)"/>{{question.answer1}}
</label>
</div>
<div class="option" id="Answer2">
<label class="Answer2">
<input value= Answer2
type="checkbox"
(change) ="CheckAnswer(value)"/>{{question.answer2}}
</label>
</div>
<div class="option" id="Answer3">
<label class="Answer3">
<input value= "Answer3"
name="Answer3"
type="checkbox"
(change) ="CheckAnswer(value)"/>{{question.answer3}}
</label>
</div>
<div class="option" id="Answer4">
<label class="Answer4">
<input value= "Answer4"
type="checkbox"
(change) ="CheckAnswer(value);"/>{{question.answer4}}
</label>
</div>
</div>
But now i am trying to give the value of the one that has been pressed to my TS file with the method CheckAnswer(value).
This is my method in TS to test if the value is given . In my console it keeps saying value is undefined
CheckAnswer(value : string) {
console.log(value);
}

You're getting undefined because value is undefined in CheckAnswer(value) in the HTML.
Try this for all your inputs:
<input value= "Answer4"
type="checkbox"
(change)="CheckAnswer($event);"/>{{question.answer4}}
And the method:
CheckAnswer(event : any) {
console.log(event); // should log something now
console.log(event.target.value); // should be the value of the checkbox
}

Related

checkbox form validation - jquery

$(document).ready(function(){
$('#update2').click(function(){
var checkboxes = $('input[name="checkbox1"], input[name="checkbox2"]');
checkboxes.on("change", function(e){
checkboxes[0].setCustomValidity(checkboxes.filter(":checked").length ? '' : 'please select at least one option to procees')
}).change
});
});
$(document).ready(function(){
$('#select2').click(function(){
var checkboxes2 = $('input[name="choose1"], input[name="choose2"]');
checkboxes2.on("change", function(e){
checkboxes2[0].setCustomValidity(checkboxes2.filter(":checked").length ? '' : 'please select at least one option to procees')
}).change
});
});
<form>
<input type="radio" id="update" name="update-button" value="1">
<input type="radio" id="update2" name"update-button" value="2">
<input type="checkbox" id="check1" name="checkbox1" value="3">
<input type="checkbox" id="check2" name="checkbox2" value="4">
<input type="submit" name="submit-button">
</form>
<form>
<input type="radio" id="select" name="select-button" value="10">
<input type="radio" id="select2" name"select-button" value="11">
<input type="checkbox" id="box1" name="choose1" value="12">
<input type="checkbox" id="box2" name="choose2" value="13">
<input type="submit" name="submit-select">
</form>
Im trying to set custom validation to the checkboxes. so when i select the radio button with the id "select2", the checkboxes with diffrent name "choose1" and "choose2" one of them is required before submitting the form. it is working perfieclty in the first example with "update2" and "checkbox1","checkbox2" checkboxes. but i have no idea why it is not working in the second example with the "select2" and "choose1", "choose2" checkboxes.

For loop - bind dynamic key to the #id

I have a for loop that displays the data into multiple radio-buttons (bootstrap):
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" id="choice.id"> [[choice.content]]
</label>
</div>
As you can see, I wanted to use the choice.id for id="..." in each button, technically it should look something like this:
<input type="radio" name="optradio" id="1"> Choice1
<input type="radio" name="optradio" id="2"> Choice2
<input type="radio" name="optradio" id="3"> Choice3
But it renders it with the actual string choice.id:
<input type="radio" name="optradio" id="choice.id"> Choice1
<input type="radio" name="optradio" id="choice.id"> Choice2
<input type="radio" name="optradio" id="choice.id"> Choice3
Forgive my naiveness. Any help/advices? Thanks a lot!
It renders with choice-id string because you add plain string as the id value, not a variable value
You can use v-bind directive or the shorthand for v-bind -> :id
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" v-bind:id="choice.id"> [[choice.content]]
</label>
</div>
using shorthand <input type="radio" name="optradio" :id="choice.id">
To answer your questions in the comments.
You can ' separate 'the radios by adding them in a ' group ' using the name attribute. Radios with the same name attribute are in one group. Changing their values won't affect other radios in other groups ( with different name attributes ). See example below.
Or you can use vue v-model to separate and get the selected options.
new Vue({
el: "#radio-app",
data: {
question1: '',
question2: ''
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://github.com/vuejs/vue-devtools"></script>
Question1:
<input type="radio" name="question1" id="q1choice1" value="choice1"/>
<input type="radio" name="question1" id="q1choice2" value="choice2"/>
Question2:
<input type="radio" name="question2" id="q2choice1" value="choice1"/>
<input type="radio" name="question2" id="q2choice2" value="choice2"/>
<hr />
<h2> Or with VUE v-model </h2>
<div id="radio-app">
Question1:
<input type="radio" id="q1choice1vue" value="choice1" v-model="question1">
<input type="radio" id="q1choice2vue" value="choice2" v-model="question1">
Question2:
<input type="radio" id="q2choice1vue" value="choice1" v-model="question2">
<input type="radio" id="q2choice2vue" value="choice2" v-model="question2">
<div>Question 1 selected answer: {{ question1 }}</div>
<div>Question 2 selected answer: {{ question2 }}</div>
</div>
Check more here
VUE template syntax
v-bind directive
Use: v-bind:id="choice.id" or the short version :id="choice.id"

Radio Buttons in ngForm Angular2

I have two radio buttons on my HTML page for gender selection (M or F).
How to retrieve which button was clicked in my typescript file? Here's the code
<form>
<label> Gender </label>
<br>
<input type="radio" required name='gender' value='Male' [(ngModel)]='gender'>Male
<br>
<input type="radio" required name='gender' value='Female' [(ngModel)]='gender'>Female
<br>
<button id="signup" class="btn btn-primary" type="submit">Signup</button>
</form>
On clicking the button, I want to assign M or F to a string in my typescript file.
You code is correct. No need to change anything.
This is your template code.
<form (ngSubmit)="onSubmit()">
<label>Gender</label>
<input type="radio" required name='gender' value='Male' [(ngModel)]='gender'>Male
<input type="radio" required name='gender' value='Female' [(ngModel)]='gender'>Female
<button id="signup" type="submit">Signup</button>
</form>
In your .ts file,
export class TestComponent implements OnInit {
gender: string = "Male";
onSubmit() {
console.log(gender);
}
}
This will give you the selected gender value.
Because you have used two way binding. It updates the ngModel whenever the input changes.
you can retrieve which button click by add ngModelChange method
<label> Gender </label>
<br>
<input type="radio" required name='gender' value='Male' [(ngModel)]='gender' (ngModelChange)="youMethodName(gender)">Male
<br>
<input type="radio" required name='gender' value='Female' [(ngModel)]='gender' (ngModelChange)="youMethodName(gender)">Female
<br>
</div>
<button id="signup" class="btn btn-primary" type="submit" >Signup</button> </form>
in your ts file
youMethodName(model) {
console.log("TCL: youMethodName -> model", model)
}
Although, answer is already accepted. I want to give solution of this problem if you do not want to use Two way data binding. this way is useful for breaking down two way data binding. So, there is no need for [(ngModel)] . Complete Working Demo found here in StackBlitz Link
Your HTML ...
<form>
<fieldset id="group1">
<input type="radio" (change)="handleChange($event)" name="group1" value="Male"/>Male
<input type="radio" (change)="handleChange($event)" name="group1" value ="Female"/> Female
</fieldset>
<div *ngIf="_prevSelected"> {{ _prevSelected}} </div>
<button (click)="click()">Show</button>
<div *ngIf="clicked"> <span> selected value is {{_prevSelected}} </span></div>
</form>
Your class file is..
private _prevSelected: any;
clicked:boolean;
handleChange(evt) {
let target = evt.target;
if (target.checked) this._prevSelected = target.value;
}
click(){
this.clicked =true;
}

Getting 1 or 0 value in materialize's switch class using Typescript

How do I get a value of 1 if it is checked and a value of 0 if not using Typescript? I am using a materialize framework.
Here's the code for it:
<div class='switch'>
<label>
Deactivate
<input name='switch_Activate' type='hidden' value='0'>
<input id='active' name='switch_Activate'
type='checkbox' onclick='onActive(this.id)' checked value='1'>
<span class='lever'></span>
Activate
</label>
</div>
function
onActive(){
}
Thank you!
You can use type="radio" inputs and bind its to ngModel
<input type="radio" name="switch_Activate" [value]="0" [(ngModel)]="switchModel">
<input type="radio" name="switch_Activate" [value]="1" [(ngModel)]="switchModel">

If checkbox unchecked Corresponding field values are posted! AngularJS

I have a checkbox ,if checked it has two fields (select tag ,and a textbox).
I also have a multiselect input field. Now out of checkbox and multiselect input atleast one of fields should be required. Problem here is if i Uncheck checkbox values in corresponding fields are posted.. Important checkbox value is scoped variable not to be sent to backend. Please help ..Thanks in Advance.
<div class="col-md-12">
<div class="checkbox c-checkbox">
Config.
<label><input name="checkbox" type="checkbox" checked="" value=""
ng-model="isChecked"
ng-required="!(isChecked|| trapList.length)"
/>
<span class="fa fa-check"></span></label>
</div>
<span ng-show="formStep2.$submitted && formStep2.checkbox.$invalid"
class="text-danger">This field is required</span>
</div>
<div ng-if="isChecked==true">
<div class="col-md-6" ng-if="isChecked==true">
<label class="control-label">SNMP Config.</label>
<select name="snmpConfig" id="snmpConfig"
ng-required="isChecked"
ng-model="$parent.$parent.snmpConfig"
class="chosen-select form-control">
<option value="" disabled selected>Select the SNMP COnfig.
</option>
<option value={{snmpConfig._id}} ng-repeat="snmpConfig in snmpConfigs">
{{snmpConfig.name}}
</option>
</select>
<span ng-show="formStep2.$submitted && formStep2.snmpConfig.$invalid"
class="text-danger">This field is required</span>
</div>
<div class="col-md-6" ng-if="isChecked==true">
<label class="control-label">Get Frequency
<small> (in milliseconds)</small>
</label>
<!--ng-required="protocol == 'SNMP'"-->
<input type="text" name="snmpGetFrequency" id="snmpGetFrequency"
class="form-control"
ng-required="isChecked"
onkeypress='return event.charCode >= 46 && event.charCode <= 57'
maxlength="20" ng-model="$parent.$parent.snmpGetFrequency"
ui-validate="'$value >= 1000'"/>
<span ng-show="formStep2.$submitted && formStep2.snmpGetFrequency.$invalid"
class="text-danger">Minimum required value = 1000</span>
</div>
</div>
<div class="col-md-12">
<label>Select Traps</label>
<select id="trapList" name="trapList" ng-model="$parent.$parent.trapList"
ng-required="!(isChecked||trapList.length) "
chosen="" class="form-control"
multiple="multiple">
<option value="{{trap._id}}" ng-repeat="trap in snmpTraps">{{trap.name}}
</option>
</select>
<span ng-show="formStep2.$submitted && formStep2.trapList.$invalid"
class="text-danger">This field is required</span>
<!--<span ng-show="form.validateInput('chosen', 'required')" class="text-danger">This field is required</span>-->
</div>
</div>
So if i uncheck checkbox corresponding fields should become empty or undefined, it should not be posted.
check this code , it may be help you
function Ctrl($scope) {
$scope.billing_is_shipping = true;
$scope.checked = function(){
console.log($scope.billing_is_shipping)
}
}
<div ng-app ng-controller="Ctrl">
<div ng-include src="'template.html'"></div>
</div>
<script type="text/ng-template" id="template.html">
<input type="checkbox" ng-model="$parent.billing_is_shipping" ng-change="checked()"/>
</script>
You can use ng-model with the checkbox, then use ng-disabled to listen to that model belonging to the checkboxes.
<input type="checkbox" value="Other" ng-model="myCheckboxes['other']" />
<input type="text" ng-disabled="myCheckboxes['other']" />