Styling selected AngularUI radio button - html

Does anyone know what is the proper CSS selector for selected (:checked) AngularUI's radio button? I tried "checked", "selected", "active", but none of this does the work. I need to find a similar selector as :checked for HTML radio button.
Every useful answer / JSFiddle is highly appreciated and evaluated.
Thank you.

If it's styling you're after then use ng-class bound to the ng-model:
<input type="checkbox" ng-model='stuff' ng-class='{class1: stuff, class2: !stuff}'>
and add css rules for classes class1 and class2

Since you are using angularjs,instead of using jquery selectors like :checked or :active,you can just use the angularjs functionalities to detect the active checkbox.
<div class="form-group">
<label class="checkbox-inline">
<input type="checkbox" name="favoriteColors" ng-click="yourFunction()" ng-model="formData.favoriteColors.red"> Red
</label>
<label class="checkbox-inline">
<input type="checkbox" name="favoriteColors" ng-model="formData.favoriteColors.blue"> Blue
</label>
<label class="checkbox-inline">
<input type="checkbox" name="favoriteColors" ng-model="formData.favoriteColors.green"> Green
</label>
</div>
cosider this as your checkboxes,now in your js (ie)your controller
var formApp = angular.module('formApp', [])
.controller('formController', function($scope) {
// we will store our form data in this object
$scope.formData = {};
$scope.yourFunction = function() {
/**function to call when a checkbox with model formdata.favourite colors.red is selected **/
};
});
Notice I have include functions for only one checkbox.You can do it for each of these check boxes
You might find this fiddle useful
http://jsfiddle.net/ShinyMetilda/C9V39/
Also this example in the documentation will be usefull
https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D

Related

How to make one button default radio button when html code does not contain options directly

How to make one button default radio button when html code does not contain options directly, data coming directly from server.
In my project I have one radio button field "Applies To".
The radio button options are not mentioned in the code as you see, I should make one option select by default, if options are not directly mentioned in the code.
How can I make one button default in this case?
<div class="row row-margin">
<label style="margin-top: 34em;margin-left: -1em;">Applies To:</label>
<label class="input" *ngFor="let question of QuestionnaireAppliesTo">
<label class="radio">
<span class="tableCellDisplay customRadioInline">
<input type="radio" [(ngModel)]="questionnaire.EntityTypeUniqueID"
name="AppliesTo" class="radio-custom" [value]="question.UUID" id="type_{{question.UUID}}">
<label for="type_{{question.UUID}}" class="radio-custom-label">{{question.Name}}</label>
</span>
</label>
</label>
</div>
My typescript code:
getQuestionnaireCategories() {
this.lookupValues.then((response: any) => {
this.QuestionnaireAppliesTo = response.QUESTIONNAIRECATEGORIES;
});
};

HTML - How can I store my custom radio button into my localStorage?

Okay, so I got a custom radio button:
<label class="radiobtn" id="region">EUW
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
And I'd like to store the text next to it (EUW) into a localStorage item called "region", when the radio button is checked.
There are multiple radio buttons of this and i want to do it for all of them.
Each custom radio button has another value (EUW, EUNE, JP, KR, etc...)
Everything else is similar.
So I kinda tried to figure out how I could do that, but I couldn't came across a good solution.
Do you guys have some good solutions? :O
Thank you for any help, I appreciate it.
Cheers!
You create custom radio buttons all with same class radiobtn, then in JQuery on radiobtn class click event check if radio button is checked, get label text and store it in localStorage.
JSFiddle
https://jsfiddle.net/mLst49h8/
HTML
<label class="radiobtn" >EUW
<input type="radio" name="radio" >
<span class="checkmark"></span>
</label>
<label class="radiobtn" >EUNE
<input type="radio" name="radio" >
<span class="checkmark"></span>
</label>
JQuery
$( ".radiobtn" ).click(function() {
if($(this).children("input").is(":checked")) {
// alert( $(this).text());
var region = $(this).text();
localStorage.setItem("region", region);
region = localStorage.getItem("region");
alert(region);
}
});

How to disable a checkbox based on conditions in angular 6?

My html code,
<div>
<div>
<input type="checkbox" id="Checkbox0" name="cCheckbox0" class="custom-control-input" (change)="checkSelected(checkBox[0].label)">
<label class="label" for="Checkbox0" >first</label>
</div>
<div>
<input type="checkbox" id="Checkbox1" name="cCheckbox1" class="custom-control-input" (change)="checkSelected(checkBox[1].label)">
<label class="label" for="Checkbox1" >first</label>
</div>
<div>
<input type="checkbox" id="Checkbox2" name="cCheckbox2" class="custom-control-input" (change)="checkSelected(checkBox[2].label)">
<label class="label" for="Checkbox2" >first</label>
</div>
</div>
<div>
<div>
<input type="checkbox" id="Checkbox3" name="cCheckbox3" class="custom-control-input" (change)="checkSelected(checkBox[3].label)">
<label class="label" for="Checkbox3" >first</label>
</div>
<div>
<input type="checkbox" id="Checkbox4" name="cCheckbox4" class="custom-control-input" (change)="checkSelected(checkBox[4].label)">
<label class="label" for="Checkbox4" >first</label>
</div>
<div>
<input type="checkbox" id="Checkbox5" name="cCheckbox5" class="custom-control-input" (change)="checkSelected(checkBox[5].label)">
<label class="label" for="Checkbox5" >first</label>
</div>
</div>
Likewise I have two more separate divs in the same html file which contains checkboxes. What I need to do is onclick of first checkbox in first div ,I need to disabled every other checkboxes from the first div,second div & third.
As I'm totally new to angular I have no idea how to disable here. I have tried using ng-disabled but it seems not working. Can someone help me with this?
For Angular 2+, you can enable / disable the checkbox by setting the disabled attribute on the input type=checkbox
Use: [attr.disabled]="isDisabled ? true : null"
Note here that [attr.disabled]="isDisabled alone will not work. You will need to explicitly set disabled attribute to null for removing disabled attribute from the disabled checkbox.
<input type="checkbox" [attr.disabled]="isDisabled ? true : null" />
ng-disabled is AngularJS syntax. You can use [disabled] input for disable checkboxes.
<input [disabled]="isDisabled" = type="checkbox" id="Checkbox0" name="cCheckbox0" class="custom-control-input" (change)="checkSelected(checkBox[0].label)">
in .ts isDisabled : boolean;
Optional thing
You can use Angular Material for your developments. because it has many advantages. And also it has well defined API.
<mat-checkbox> provides the same functionality as a native <input type="checkbox"> enhanced with Material Design styling and animations.
Angular Material
You can use the [disable] attribute your input[type="checkbox"] tag.
For eg: -
<input [disabled]="isDisabled" type="checkbox" id="Checkbox3" name="cCheckbox3" class="custom-control-input" (change)="checkSelected(checkBox[3].label)">
isDisabled variable will hold the boolean value in your .ts
For larger, more complex forms I highly recommend using a reactive form. With a reactive form, you can use [disabled]="condition" where the condition is something like whateverCheckbox.value.
UPDATE:
I updated my answer to use whateverCheckbox.value as opposed to whateverCheckbox.checked. This approach only works with reactive forms. I highly recommend using reactive forms for larger, more complex forms where you may need more detailed, personalized control over the elements of the form. Reactive forms are built around observable streams, where form inputs and values are provided as streams of input values, also while giving you synchronous access to the data.
Here is the documentation: https://angular.io/guide/reactive-forms
Once you have the form set up as a reactive form, a form control instantiated and bound to the checkbox input form element, you should be able to access the value of the checkbox as indicated above.
UPDATE 2: You don't necessarily need to use a form either to take advantage of a reactive form control.
In your component.ts file import FormControl from #angular/forms as below:
import { FormControl } from '#angular/forms';
Then declare the form control in the component class, as such:
checkbox1 = new FormControl('');
Then in your template, simply bind that FormControl to the input element as such:
<input type="checkbox" [formControl]="checkbox1">
and then you should be able to access that value anywhere using:
checkbox1.value
If you are using reactive forms you can also disable the checkbox from the component like this
import { FormBuilder, FormGroup } from '#angular/forms';
constructor(private _fb: FormBuilder) { }
myForm: FormGroup;
ngOnInit(): void {
this.myForm = this._fb.group({
myCheckBoxInput: [{value: 1, disabled: true}],
});
}
in reactive form, you can disable the checkbox like so
this.formGroup.get('Checkbox1').disable({ onlySelf: true });

after checkbox checked, redirection to a textbox

Is it possible that when I check a checkbox it redirects me directly to a textbox ?
Like if I did a TAB when I check it.
And if possible only with HTML and CSS
You can't do it with just HTML & CSS
but here is a solution using javascript:
window.onload = function() {
var checkbox = document.getElementById("agree"),
textbox = document.getElementById("textbox");
checkbox.onclick = function() {
if(this.checked) {
textbox.focus();
}
};
};
demo
Maybe the minimal implementation would be:
<form name="f1">
<input id="in1" name="in1" type="checkbox" onClick="if(this.checked) document.f1.in2.focus()" /> Check option <br/>
<input id="in2" name="in2" type="text" />
</form>
Fiddle demo
The simpliest way is to use TabIndex attribut in the input element
<input type="checkbox" Tabindex="1"> </input>
<br>
<br>
<input type="text" Tabindex="2"> </input>
I hope this might help you

HTML Label "For" Value

I have a label tag that I am trying to link to an input type checkbox tag. I have multiple labels and multiple checkbox inputs, and they all have the same id and the same name, but different values. Can someone instruct me as how to construct a label that links to a value rather than an id? So this:
<label for="8994"></label>
Would link to:
<input id="member_ids_" name="member_ids[]" type="checkbox" value="8994">
Or is this not possible?
The label's for attribute must match the ID of the <input> element it refers to. In your case it would be something like:
<label for="member_id_8994"></label>
<input id="member_id_8994" name="member_ids[]" type="checkbox" value="8994">
The 'for' for the form element must match with the ID of the same form element.
<label for="id_1"></label>
<input id="id_1" name="member_ids[1]" type="checkbox" value="8994">
<label for="id_2"></label>
<input id="id_2" name="member_ids[2]" type="checkbox" value="8994">
<label for="id_3"></label>
<input id="id_3" name="member_ids[3]" type="checkbox" value="8994">
<label for="id_3"></label>
<input id="id_3" name="member_ids[4]" type="checkbox" value="8994">
Your DOM elements must have different IDs.
Even if each ID is just set to whatever that value is... ...or whatever.
They can not have the same ID.
Once you've got that out of the way, setting for on a label becomes really simple.
I doubt if that is possible. Label's for are tied to the id attribute of inputs. One way to do achieve your objective maybe through javascript, knockout's declarative bindings for instance.
check it our here: http://knockoutjs.com/documentation/introduction.html
Something along this line:
<label data-bind="click: myInput"></label>
<input type="checkbox" id="hello">
<script type="text/javascript">
var myInput= {
//implement the function to bind the label to the input#hello here
}
};
</script>
http://knockoutjs.com/documentation/click-binding.html
A jQuery solution that probably doesn't work.
$(function() {
ModifyLabels({target: $('input')});
$('input').change(ModifyLabels);
});
function ModifyLabels(eventObject) {
var inputs = $(eventObject.target);
input.each(function(i, input) {
$('label').each(function(i, label) {
if ($(label).attr('for') == $(input).val()) {
$(input).attr('id', $(input).val());
}
});
});
}