http radio-Attribut using angular - html

I have the following code
<div>Yes or no?</div>
<input type="radio" id="Nein" name="isUsedFor [(ngValue)]="isNotUsedFor" [(ngModel)]="isNotUsedFor">
<label for="no">Nein</label><br>
<input type="radio" id="Ja" name="isUsedFor" [(ngValue)]="isUsedFor" [(ngModel)]="isUsedFor">
<label for="yes">Ja</label>
</div>
My problem is, when I click on Nein, then Ja will be selected and not the Nein, and when I click on Ja then the Nein will be selected.
Here for example I have clicked on Nein and Ja will be selected and when I submit, then Ja will be submitted.
Do you have a idea to solve my inconvenience?
Thank you in advance.
Here is my .ts file:
public isNotUsedFor: string = "Nein";
public isUsedFor: string = "Ja";

Demo
You should use one model for both input and give value to input elements.
<div>Yes or no?</div>
<input type="radio" id="Nein" name="isUsedFor" value="Nein" [(ngModel)]="isUsedFor"/>
<label for="no">Nein</label><br>
<input type="radio" id="Ja" name="isUsedFor" value="Ja" [(ngModel)]="isUsedFor"/>
<label for="yes">Ja</label>
<br>
{{isUsedFor}}
in component
isUsedFor: string = "Ja";

Related

How to change boolean value if checkbox is cheked?

I would like to change a boolean value inside the component file with a simple checkbox. If it is not checked it should remain false, otherwise it should be true.
Component:
export class UsersComponent {
public Personal: boolean = false;
}
HTML:
<label>
<input type="checkbox" name="Personal" [(ngModel)]="Personal"/>
Personal
</label>
Since you have added 2 way binding in html [(ngModel)]="Personal", it should assign automatically
check via
<label>
<input type="checkbox" name="Personal" [(ngModel)]="Personal"/>
{{Personal}}
</label>
You can use these to toggle the value of the Personal
HTML:
<label>
<input type="checkbox" [checked]="Personal" (change)="mPersonal = !Personal" /> Checked + Change
{{Personal}}
</label>
Correct For 2 way binding :
<label>
<input type="checkbox" name="Personal" [(ngModel)]="Personal" />
{{Personal}}
</label>

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;
}

Can a <FORM> submit radio-button's <LABEL> as VALUE?

We currently use the following syntax for radio buttons:
<input type="radio" id="opt1" name="option" value="opt1" required/>
<label for="opt1">Description of Option One</label>
<input type="radio" id="opt2" name="option" value="opt2" required/>
<label for="opt2">Description of Option Two</label>
The query-processing script receives the string "opt1", which it then needs to convert to the full-text description of the option. In PHP-speak, I get:
$_POST['option'] => "opt1"
I'd like to save that step and have the full text of the description to be submitted as the value:
$_POST['option'] => "Description of Option One"
Can this be done with HTML alone -- without resorting to JavaScript-rewriting hacks and without duplicating the description text in the HTML? Thanks!
Unfortunately not.
If you have control over the form, the best solution is to use the description for the value:
<input type="radio" id="opt1" name="option" value="Description of Option One" required/>
<label for="opt1">Description of Option One</label>
<input type="radio" id="opt2" name="option" value="Description of Option Two" required/>
<label for="opt2">Description of Option Two</label>
If you don't have control over the form, then javascript is your only solution, you could use a function like the below (either inside an onload event for the page or an onsubmit event on the form:
function radioUpdate() {
document.querySelectorAll('radio').forEach(function(input) {
input.value = document.querySelector('label[for="' + input.id + '"]').text();
});
};
No, it can't.
Consider generating the HTML from your server side code in the first place. You could write a PHP function that takes the label/value as a single argument.

While clicking label event is fired twice in angularjs

This is HTML code:
<label for="like" ng-click="copyRec()">
<input type="checkbox" id="like" value="val"/>
Click
</label>
In my Angular controller:
$scope.copyRec = function() {
console.log("inside");
};
While clicked its firing twice. Can anyone help me out in solving this?
in your HTML code
<input type="checkbox" ng-click="copyRec()" id="like" value="val"/>
<label for="like" >Click</label>
Your function is firing twice because copyRec() is call on your label and on your input (checkbox).
I would suggest to move ng-click to the input only:
<label for="like">
<input type="checkbox" id="like" value="val" ng-click="copyRec()"/>
Click
</label>

How to add values in HTML forms?

How would i add the "value" that are selected from radio boxes in html forms? So when someone selects an option it would add the other "values" onto it and total that it at the bottom of the page. And does anyone know if it could add "names" total "values" onto it as well? thanks
My code looks like this:
<h3><u>Title</u></h3><br>
<form action="">
<input type="radio" name="num" value="0">Text<br>
<input type="radio" name="num" value="2">Text<br>
<input type="radio" name="num" value="80">Text<br>
<input type="radio" name="num" value="110">Text<br>
<input type="radio" name="num" value="85">Text<br>
<input type="radio" name="num" value="120">Text<br>
</form>
You cannot. By definition, a set of radio buttons with the same name attribute contributes at most one value to the data set, the one corresponding to the selected button.
If you want something else, you should handle that server side, or use other types of controls, or redesign the entire approach.
Working example :
(using a Javascript library, jQuery, but could be done in plain JavaScript)
You mainly have to change your inputs to type="checkbox" in the HTML
What code does : when a checkbox's state is modified, all checked checkboxes' value are summed up in the last input field I've added
The checkboxes are targetted by looking for "num" in their name, if you remove that the checkbox won't be taken into account by the script.
$(function() {
$("input[name*='num']").on("change", function() {
var total = 0;
$("input[type='checkbox']:checked").each(function() {
total += Number($(this).val());
});
$("#total").val(total);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>
<u>Title</u>
</h3>
<br>
<form action="">
<input type="checkbox" name="num0" value="0">Add 0<br>
<input type="checkbox" name="num2" value="2">Add 2<br>
<input type="checkbox" name="num80" value="80">Add 80<br>
<input type="checkbox" name="num110" value="110">Add 110<br>
<input type="checkbox" name="num85" value="85">Add 85<br>
<input type="checkbox" name="numwhateveryoulike" value="120">Add 120<br>
Total <input type="text" value="0" id="total">
</form>