Focus Label when Input is focussed with angular - html

I have a webpage which uses radio button labels as buttons, with inputs hidden
<li ng-repeat="amount in amounts | orderBy: amount" ng-mouseenter="$parent.hoverPrompt = $parent.amounts[$index].donationPrompt" ng-mouseleave="$parent.hoverPrompt = null">
<input type="radio" name="amount_{{amount.suggestedAmount}}" id="donate_{{amount.suggestedAmount}}" ng-value="{{amount.suggestedAmount}}" ng-model="donate.amount" string-to-number />
<label class="as-button" for="donate_{{amount.suggestedAmount}}">${{amount.suggestedAmount}}</label>
</li>
I want my labels to be visibly highlighted when they or their associated hidden inputs are tabbed over.
I found a ready jquery snippet but would prefer to do it in angular.

I found a CSS only solution, since in my case the label comes after the input:
.amounts input:focus + label {box-shadow: 0 0 2px $brand-primary;}
<ul class="list-group amounts">
<li ng-repeat="amount in amounts | orderBy: amount" ng-mouseenter="$parent.hoverPrompt = $parent.amounts[$index].donationPrompt" ng-mouseleave="$parent.hoverPrompt = null">
<input type="radio" name="amount_{{amount.suggestedAmount}}" id="donate_{{amount.suggestedAmount}}" ng-value="{{amount.suggestedAmount}}" ng-model="donate.amount" string-to-number ng-focus="$parent.hoverPrompt = $parent.amounts[$index].donationPrompt" ng-blur="$parent.hoverPrompt = null"/>
<label class="as-button" for="donate_{{amount.suggestedAmount}}">${{amount.suggestedAmount}}</label>
</li>

Here is a plunkr that uses ng-focus to apply highlighting to the label when an associated input is focused. ng-blur is used to remove the styling when an element loses focus.
I am simply setting a scope variable called 'highlight' to true when the element is focused, and to false when the element loses focus (blur).
Ng-class is used to conditionally apply css based on my 'highlight' variable.
HTML
<div ng-repeat="data in posts">
<label ng-class="highlight == true ? 'highlight' : '' ">{{data.name}}</label>
<input type="radio" ng-focus="highlight = true" ng-blur="highlight =false" ng-class="highlight == true ? 'highlight' : '' " />
</div>
CSS
.highlight { background-color: yellow;}
ng-focus will not; however, work on a label element. It is limited to inputs.

In label you can use
<label class="as-button" ng-class="{'active': donate.amount == amount.suggestedAmount}"for="donate_{{amount.suggestedAmount}}">${{amount.suggestedAmount}}</label>
Using ng-class="{'active': boolean} is consistent work on old and new angularjs versions because it base on html class, and also it not stealing focus from another element in your page or lose focus effect if use use tab or click another one. Also I recommend using button group for for those radio button like:
<div class="btn-group" data-toogle="buttons-checkbox">
<label ng-repeat="type in petTypes" class="btn btn-primary" ng-class="{'active': pet.type==type}" ng-click="setPetType(pet,type)">
<input type="radio" name="{{pet.id}}" value="{{type}}" class="sr-only" ng-checked="pet.type==type" required>{{type}}
</label>
</div>
This from my code for my pet control app and it generate new pet when clicking a button, this part is about radio button and it work just fine. Another note is the name is what control which group the input belong to.

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

Add a checked attribute to a dynamic radio button

I'm building a *ngFor dynamic radio button. I need to add some kind of attribute that can be used to determine if the button is active or not. This is not for the purpose of functionality, the selection builds the property on my reactive form fine. The purpose is to satisfy testing.
<label *ngFor="let order of orders">
<input formControlName="orders" type="radio" name="orders" [value]="order.id" />
{{order.name}}
</label>
I've tried adding a checked attribute but this just adds the attribute to all radios. I thought angular provided a ngChecked property, but I think that was for AngularJS.
Any solution to determine/show that the selected radio is active would be a solution.
https://stackblitz.com/edit/angular-t2gd4g
simply by get the value of the form control orders you will be applied to know the selected one or in case of apply a spesifect design you can add a checked class for the selected one
<label *ngFor="let order of orders"
[ngClass]="{'checked':form.get('orders').value == order.id}">
<input formControlName="orders" type="radio" name="orders" [value]="order.id" />
{{order.name}}
</label>
another way to get the selected order by create a property
get selectedOrder() {
const selected =this.form.get('orders').value;
if (selected) {
return this.orders.find(o => o.id == selected )
} else {
return null
}
}
demo 🚀
In your .ts file declare changeHandler() function like this :
changeHandler(index){
console.log(`radio-button ${index+1} is active`);
}
In your .html file, in your *ngFor declare index which can be used to find the current state of checkbox. So the following demo shows which checkbox is active :
<label *ngFor="let order of orders; let i = index;">
<input formControlName="orders" type="radio" name="orders" [value]="order.id" (ngModelChange)="changeHandler(i)"/>
{{order.name}}
</label>
Demo : enter link description here
Note : see console to see active button

How to double click to tick a checkbox?

I use a Shopping List with a changeStatus function to update an element on click. Elements are automatically filtered (non ticked at the top). Sometimes I missclick on an element so it becomes ticked and is automatically filtered and then instantly disapears in the bottom of my list so I could not even know what element has been ticked (too bad when doing shopping...).
I would like to prevent this by having a mandatory double click to tick/untick element, but I don't know how to deal with it with checkbox.
<fieldset class="items-list">
<label class="items-list-item" ng-repeat="item in items | filter : filterItem">
<input
type="checkbox"
value="{{item.STATUS}}"
ng-checked="item.STATUS==2"
ng-click="changeStatus(item.ID,item.STATUS,item.ITEM)"
class="items-list-cb"/>
<span class="items-list-mark"></span>
<span class="items-list-desc" ng-class="{strike:item.STATUS==2}">{{item.ITEM}}</span>
<a ng-click="deleteItem(item.ID)" class="pull-right red"><i class="fa fa-minus-circle"></i></a>
</label>
</fieldset>
Try this code:
$(document).ready(function() {
$(".dbl").click(function(e) {
e.preventDefault();
});
$(".dbl").dblclick(function(e) {
let myCheckbox = $("input[type=checkbox]", this);
myCheckbox.prop("checked", !myCheckbox.prop("checked"));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="dbl"><input class="dbl" type="checkbox">double click to tick/untick element</label>
The code below ignores the single click with the help of onclick="return false" and toggles the tick on double click with the help of ondblclick="this.checked = !this.checked"
<input type="checkbox" onclick="return false" ondblclick="this.checked = !this.checked" />
This could be done by adding onclick and ondblclick to the input tag. When clicked, the onclick removes the check, but if double-clicked the ondblclick adds the check.
<p>Double-click ads a check, while single click removes the check.</p>
<input type="checkbox" onclick="this.checked = false" ondblclick="this.checked = true">

Semantics of HTML element that acts on `input`

I have a set of checkboxes with the following structure:
<div>
<div>
<label><input type="checkbox" value="a1"/>A1</label>
</div>
<div>
<label><input type="checkbox" value="a2"/>A2</label>
</div>
...
</div>
The checkboxes are only used for UI control (not for a form submission). I have an associated html element that has an onclick jQuery function that clears all the checkboxes. This element is currently just a div. Semantically, are there any best practices to say whether this should be a button, an a (with no href value) or continue to be a div?
You should use a button element in the button state (or an input element in the button state).
Why not a? Because it should only be used for links to resources.
Why not div? Because it isn’t focusable by default (for keyboard users), and because it doesn’t come with an implicit WAI-ARIA role (for accessibility), so you would have to add both features manually, essentially recreating an element that already exists: button.
Semantics is a dying dodo bird. Having said that here's my attempt to meet standards that seem to be largely ignored. I just use whatever element is best suited for the task and seems logical, generic divs and spans being the last to be considered. I believe keeping code from presentation and markup was a major objective as well, so use addEventListener instead of attribute event handlers like onclick
SNIPPET
var allchx = document.getElementById('allChecks');
allchx.addEventListener('change', function(e) {
this.checked ? checkUncheck(true) : checkUncheck(false);
}, false);
function checkUncheck(chxBool) {
var chxList = document.querySelectorAll('input[name^="question"]');
var qty = chxList.length;
for (let i = 0; i < qty; i++) {
chxList[i].checked = chxBool;
}
}
<form id='survey' name='survey' action='http://httpbin.org/post' method='post'>
<fieldset class='checkboxSet'>
<legend>Product Survey</legend>
<label for='allChecks'>
<input id='allChecks' type='checkbox'>Check/Uncheck All</label>
<hr/>
<ol>
<li>
<label for='question1'>
<input id='question1' name='question1' type='checkbox' value='smoker'>Do you smoke?</label>
</li>
<li>
<label for='question2'>
<input id='question2' name='question2' type='checkbox' value='lard eater'>Do you eat lard?</label>
</li>
<li>
<label for='question3'>
<input id='question3' name='question3' type='checkbox' value='potential customer'>Do you have explosive diareha?</label>
</li>
<li>
<label for='question4'>
<input id='question4' name='question4' type='checkbox' value='definite customer'>Would you be interested in having explosive diareha in the near future?</label>
</li>
</ol>
<input type='submit'>
</fieldset>
</form>

Styling selected AngularUI radio button

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