Semantics of HTML element that acts on `input` - html

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>

Related

Combining an <a> tag and a <label>

I can't make a and an tag combine on HTML.
I'm trying to have text that when you click on it both click a checkbox and also lead you somewhere on the page i've tried to make it like that:
<a href"#somewhere"><label for"somecheckbox">Some Text</label></a>
But only the label worked, then I tried it like that:
<label for"somecheckbox"><a href"#somewhere">Some Text</a></label>
But then only the link works, is there any way in which we can use both?
The problem is you are trying to nest interactive content. This is something you can't do via the W3C spec. See the a tag, for instance with it's permitted content.
You will need to use javascript to achieve what you want to do.
Here is a quick example:
var links = document.querySelectorAll("[data-for]");
//Set Event Handler
for(var i = 0; i < links.length; i++) {
links[i].addEventListener("click", function(event){
//Get the target cb from the data attribute
var cbTarget = this.dataset.for;
//Check the cb
document.getElementById(cbTarget).checked = true;
});
}
.head {margin-bottom: 100vh;}
<div class="head">
Click Me <input type="checkbox" id="aCheckBox" />
</div>
<div id="aTarget">A Target</div>
It's not possible to do both navigation and toggle checkbox using tags, please use javascript to focus on target when checkbox is checked.
document.getElementById("somecheckbox").addEventListener("change", function(e){
// see if it is checked
if(e.target.checked){
// and focus to specific id
document.getElementById("somewhere").focus();
}
})
When you click on the label, both the check box is checked and it goes to a location.
const label = document.querySelector('[for=checkbox]');
const checkBox = document.querySelector('[name=checkbox]');
label.addEventListener('click', function (){
checkBox.setAttribute("checked", "true");
location.href = "#location";
});
#location {
position: absolute;
bottom: -100px;
}
<label for="checkbox">
<input type="checkbox" name="checkbox"/>
Check
</label>
<p id="location"> Some location </p>
<!DOCTYPE html>
<html>
<body>
<script>
function Redirect(){
if (document.getElementById('vehicle3').checked)
{
window.location = "https://www.tutorialspoint.com";
}
}
</script>
<h1>Show checkboxes:</h1>
<input type="checkbox" name="vehicle1" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle2" value="Car"> I have a car<br>
<input type="checkbox" name="vehicle3" id='vehicle3' value="Boat" onClick='Redirect();'> I have a boat<br><br>
<input type="submit" value="Submit">
</body>
</html>
This solution is what everyone suggesting using JavaScript your problem can be solved easily. As soon as you click on the 3rd checkbox it redirects you to a new webpage

Focus Label when Input is focussed with angular

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.

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

All input field should be in same line

I am writing a html document in which the output is as follows:
I have two problems with it:
1. I want to line up the first three input fields.
2. Radio buttons are not working properly.
My html code is:
<!DOCTYPE html>
<html>
<head>
<title>
Contact Us
</title>
<link rel="stylesheet" href="styleit.css"/>
</head>
<body>
<h1> Contact Us </h1>
<form id="form">
Your Name : <input type = "text" value = "name" > </br>
Mobile no : <input type = "text" value = " Mob" ></br>
Email : <input type = "text" value = "Email"></br>
Best time to call: <input type="radio" >evening
<input type="radio">morning </br>
Languages: <br>
<input type="checkbox" > C
<input type="checkbox" > C++ <br>
<input type="checkbox" > C#
<input type="checkbox" > python<br>
<input type="checkbox" > Java
<input type="checkbox" > CSS </input> <br>
<input type="submit" value="submit">
</form>
</body>
</html>
My css code is:
form input
{
margin: 5px;
display: inline-block;
width: 150px;
}
h1
{
text-align:center;
}
What am I doing wrong ?
To fix your radio buttons is quite simple... you have to tell the browser in which "group" your radio button are, and this is done through the name attribute :
<input type="radio" name="bestTimeToCall">evening</input>
<input type="radio" name="bestTimeToCall">morning</input>
Aligning your form fiels is a bit more tricky and can actually be done several ways. One way would be to use a table and put your labels in first column and your fields in the second one. That's not optimal as you shouldn't use tables for that, but still for a beginner this is by far the easiest solution.
The best solution, but a bit more complicated, would be to use the label tag for your labels, and style this tag so that it's always the same width, meaning that the iput fields will all start at the same point, and look aligned.
Please note also that <br> can be written that way (in HTML 5) , or <br/> , but never </br>
You should add a name property to your radio and checkbox input. you can read about some useful form tags here
Regarding the alignment, there are several ways to solve this, depending on what do you think it's the best way. You could for instance, use a table to arrange this kind of field, or add classes to you input fields and align them via css. I'd recommend using bootstrap to work this kind of thing, it's a very practical solution. But again, possibilities are nearly unlimited.
<label>Your Name :</label> <input type = "text" value = "name" >
<label<Mobile no :</label> <input type = "text" value = " Mob" >
<label>Email :</label> <input type = "text" value = "Email"
</label>Best time to call:</label> <input type="radio" name="time">evening
<input type="radio" name="time">morning </br>
Add attribute - name for radio inputs.
Remove <br />
Add <label> with display: inline-block
JSFIDDLE

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