How to indicate that a group of radio buttons are required [duplicate] - html

This question already has answers here:
How to use the "required" attribute with a "radio" input field
(5 answers)
Closed 8 years ago.
I understand how to place an icon in a text field to indicate to the user that it is required and in HTML5 form get the tip indicating that the field should be filled out.
is there any similar mechanism for radio buttons and check boxes?
Below is a simple group of radio buttons that I am using.
<!DOCTYPE html>
<polymer-element name='marital-status-form'>
<template>
<style">
#col1 { order:1; }
#col2 { order:2; align-self:flex-start; }
#hr { order:3; }
</style>
<form id='form'
name='form'
on-change='{{updateModel}}'>
<section
<label for='marriedRdo' id='marriedLbl' >Married</label>
<label for='divorcedRdo' id='divorcedLbl' >Divorced</label>
<label for='singleRdo' id='singleLbl' >Single</label>
<label for='visitingRdo' id='visitingLbl' >Visiting</label>
</section>
<section >
<input id='marriedRdo'
name='status'
type="radio"
value='Married'
on-click='{{submit}}'>
<input id=divorcedRdo name='status' type='radio' value='Divorced'>
<input id='singleRdo' name='status' type="radio" value='Single' >
<input id=visitingRdo name='status' type='radio' value='Visiting'>
</section>
<hr id='hr'>
<delete-dispatch-form id='delete-dispatch-form'></delete-dispatch-form>
<button id='submit-btn'
type='submit'></button>
</form>
</template>
<script type="application/dart">
import 'package:polymer/polymer.dart' show CustomTag, observable, PolymerElement;
import 'dart:html' show Event, Node, InputElement;
#CustomTag( 'marital-status-form')
class MaritalStatusForm extends RooleElement
{
#observable String choice= '';
MaritalStatusForm.created() : super.created();
void updateModel(Event e, var detail, Node target)
{
//maritalStatus.status = (e.target as InputElement).value;
//print( encode( maritalStatus ) );
}
void submit ( Event e, var detail, Node target )
{
$['form'].onSubmit.listen( ( e )
{
e.preventDefault();
} );
}
}
</script>
</polymer-element>

A simple solution to this problem is select a radio button by default. That way, the user can either stay with the default, or pick a different option. This, in effect, 'requires' a group of radio buttons.

From a html point of view, you can add the 'required' attribute to one of the radio inputs in the group and it will require one of the options to be selected.
<input id='marriedRdo'
name='status'
type="radio"
value='Married'
on-click='{{submit}}'
required="required">
See this fiddle for a demo: http://jsfiddle.net/lickmydesign/5y4Lv6gc/

Related

Why "checked" does not make the Toggle buttons checked by default?

I have a set of toggle buttons in a custom form using the following lines:
<div class="calc-toggle-wrapper">
<input type="checkbox" :checked="element.isChecked" :value="element.value" #change="change(event, element.label)" />
<label></label>
</div>
I want to make all these toggle buttons "on" or "checked" by default. I added the following to the end of the input () tag but nothing worked:
checked
checked="checked"
checked="true"
The toggle buttons on my form are still unchecked by default. I am not sure why it is not working or what I am doing wrong here. Any suggestions will be very much appreciated. Thank you.
You can use a computed property to set the default value of the checkbox. For example:
computed: {
isChecked: {
get() {
return this.element.isChecked;
},
set(value) {
this.element.isChecked = value;
// Add the value to the form here
}
}
}
Then, in your template, you can bind the checkbox to the isChecked value using the v-model directive:
<div class="calc-toggle-wrapper">
<input type="checkbox" v-model="isChecked" :value="element.value" #change="change(event, element.label)" />
<label></label>
</div>

How to put a if this happens, then play this sound in HTML

How can I put an if answered correctly play sound: "mp3 url" in HTML?
I tried multiple ways, but they still don't work.
I am trying to do it with Tampermonkey
You'll need Javascript to check if your condition returns true.
if(answeredcorrectly) {
let mp3= new Audio('urltomp3file.mp3);
mp3.play();
}
You must use javascript for that. Try this:
if (correct) {
var sound = new Audio('file.mp3');
sound.play();
} else {
var sound_i = new Audio('file2.mp3');
sound_i.play();
}
You can't do that with just HTML, but with a little jQuery you can get it done.
/* Create a sound element our function can reference */
const the_sound = new Audio("https://freesound.org/data/previews/555/555547_1676145-lq.mp3");
the_sound.pause();
/* Attach a handler to the form.quiz_form element and listen for the submit event */
$("form.quiz_form").submit(function(e) {
/* Kill the form submit event */
e.preventDefault();
/* Get the selected form field */
/* Note $(this) refers to the jquery element attached to this handler function (form.quiz_form) */
let answer = $(this).find("input:checked").val();
/* Check the value of the answer and continue if correct */
if (answer === "correct") {
/* Check if sound not already playing and play if paused */
if (the_sound.paused) {
the_sound.play();
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--
Create a form with a set of questions. Each input element has a value of "correct" or "incorrect"
-->
<form class="quiz_form" action="#" method="post">
<h3>This is the question</h3>
<input type="radio" id="answer_1" name="quiz_answer" value="correct">
<label for="answer_1">This is the correct answer</label>
<br/>
<input type="radio" id="answer_2" name="quiz_answer" value="incorrect">
<label for="answer_2">This is the wrong answer</label>
<br/>
<input type="radio" id="answer_3" name="quiz_answer" value="incorrect">
<label for="answer_3">This is the wrong answer</label>
<br/>
<br/>
<button type="submit">Submit your answer</button>
</form>

How to make radio buttons with same value reusable?

I am a beginner to Vue, and learning something by doing. I was able to make a checkbox reusable, but getting some weird result for radio buttons.
I have the data in an array format in ProgramDesign.vue:
data() {
return {
strategies: [
"Not Important",
"Slightly Important",
"Moderately Important",
"Very Important",
"Extremely Important",
],
};
},
These are the options that get repeated on every question.
I made a separate component for the radio like this:
<template>
<div>
<span v-for="strategy in groups" :key="strategy">
<input :id="strategy" class="radio-style" name="strategy" type="radio" />
<label :for="strategy" class="radio-style-3-label">{{strategy}}</label>
</span>
</div>
</template>
<script>
export default {
props: {
groups: Array,
},
};
</script>
This is how it's used in ProgramDesign.vue:
<p>first question goes here ?</p>
<RadioButton :groups="strategies" />
<div class="line"></div>
<p>second question goes here ?</p>
<RadioButton :groups="strategies" />
I was able to get the reusable output, but when I click on the radio button for the second question, the buttons for the first question get selected. How can I fix this?
The problem is the input IDs and names are not unique between component instances, as can be seen in the rendering of your two RadioButton components (simplified for brevity):
<!-- RadioButton 1 -->
<div>
<span>
<input id="Not Important" name="strategy" type="radio">
<label for="Not Important">Not Important</label>
</span>
</div>
<!-- RadioButton 2 -->
<div>
<span>
<input id="Not Important"❌ name="strategy"❌ type="radio">
<label for="Not Important">Not Important</label>
</span>
</div>
Each label is linked to an input by matching the for and id attributes, such that clicking the label causes the linked radio input to change values. When there are multiple inputs with the same identifier, the browser links the label to the first matching input, causing the behavior you observed.
The name must also be unique between groups (RadioButton instances), since the browser creates radio groups of inputs that have matching names.
Solution
Alternatively, a label and input can be linked by putting the input inside the label, resolving the id/for duplication (and improving readability):
<label>
<input name="strategy" type="radio">
Not Important
</label>
And one way to resolve the duplicate names is to base the name on a counter incremented per instance:
<template>
<div>
<label v-for="strategy in groups" :key="strategy">
<input :name="'strategy' + groupId" type="radio">
{{strategy}}
</label>
</div>
</template>
<script>
let groupId = 0
export default {
props: {
groups: Array
},
data() {
return {
groupId: groupId++
}
}
}
</script>

How to create a popup that provides user multiples options?

Currently I am designing a user opinion page where I planned to get input from user like "Is it suitable?"
Possibly the input could be YES / NO / No Idea
The native controls such as alert() or confirm(), prompt() etc doesn't provide more than two options.
Is there a native control , that can provide more than two options..?
If not, How can I create one using angular-ui or jquery-ui?
<input type="radio" name="group1" value="yes"> Yes<br>
<input type="radio" name="group1" value="no"> No<br>
<input type="radio" name="group1" value="no_idea" checked> No Idea<br>
It's just standard html. Why you think it's need jquery or angular ?
You can use the jQuery UI dialog widgets buttons option.
It accepts either an object whose keys are the buttons's label, and value is the callback function or an array of objects defining the attributes, properties, and event handlers to set on the button.
The context of the callback functions is the dialog element. You can use the target property of event in case you want to access the button.
$(function() {
$("#dialog").dialog({
title:"Is it suitable?",
buttons: [{
text: "Yes",
click: function(event) {
$(this).dialog("close");
}
}, {
text: "No",
click: function(event) {
$(this).dialog("close");
}
}, {
text: "No Idea",
click: function(event) {
$(this).dialog("close");
}
}]
});
});
<link href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

How to convert text to uppercase while typing in input field using angular js

I know that is posible with jquery but I dont know how to do that with angular js, please any sugestion?
function mayuscula(campo){
$(campo).keyup(function() {
$(this).val($(this).val().toUpperCase());
});
}
You can also create a directive for this!
Check the code:
directive('uppercase', function() {
return {
restrict: "A"
require: "?ngModel",
link: function(scope, element, attrs, ngModel) {
//This part of the code manipulates the model
ngModel.$parsers.push(function(input) {
return input ? input.toUpperCase() : "";
});
//This part of the code manipulates the viewvalue of the element
element.css("text-transform","uppercase");
}
};
})
For its usage, here's an example:
<input type="text" ng-model="myModel" uppercase />
You could do it in HTML template or via JS using the angular uppercase filter.
<div>
<label>Input 1</label>
<input type="text" ng-model="first">{{ first | uppercase }}
</div>
If you need to change the value in-place, use toUpperCase when ever value is changed.
<div>
<label>Input 1</label>
<input type="text" ng-model="first" ng-change="text = text.toUpperCase()">
</div>
Above in preferred approaches. Here's yet another way to achieve same result using $watch but this is not recommended. See comments section.
<div>
<label>Input 2</label>
<input type="text" ng-model="second">
</div>
var unwatch = $scope.$watch('second', function(val) {
$scope.second = $filter('uppercase')(val);
}, true);
$scope.$on('$destroy', unwatch);
Related Plunker here http://plnkr.co/edit/susiRn