How do i check a Radiobutton list - javascript

How do I check a radio button?? Here is the radio button list:
<div class="md-radio-list">
<label class="md-radio" id="#ch.AnswerId">
<input name="#ch.QuestionId" onclick="handlesClick(this);" id="#id" type="radio">
<span></span>
<label for="#id">
<span class="inc"></span>
<span class="check"></span>
<span class="box"></span> #ch.AnswerText
</label>
</label>
</div>
Here the radio buttons are dynamic, They will bind to the radio button list.
I have tried :
<input name="#ch.QuestionId" checked="checked" onclick="handlesClick(this);" id="#id" type="radio">
But it's not working.

Use below to check without any event (click)
document.getElementById("#id").checked=true
Snippet
document.getElementById("#id").checked = true
<div class="md-radio-list">
<label class="md-radio" id="#ch.AnswerId">
<input name="#ch.QuestionId" onclick="handlesClick(this);" id="#id" type="radio">
<span></span>
<label for="#id">
<span class="inc"></span>
<span class="box"></span>
#ch.AnswerText</label>
</label>
</div>
HTML Solution:
Simply add checked attribute in your element
<input name="#ch.QuestionId" onclick="handlesClick(this);" id="#id" type="radio" checked>

$('input[type="radio"]').attr("checked", true); //this will check all radio buttons
$('#id').attr("checked", true); //this will check specific radio

Related

Javascript: Input type radio

How can I select input type radio by just clicking the space around it?
You could use a <label> tag:
<input type="radio" name="box" id="box">
<label for="box">Your content here</label>
Clicking on the label toggles the input.
You should be using the label element. There are two ways to use it with a radio button, as shown below.
<form>
<label>
<input type="radio" name="options">
<span>Option 1 - Inside Label</span>
</label>
<br>
<input type="radio" name="options" id="button2">
<label for="button2">
<span>Option 2 - Outside Label</span>
</label>
</form>

Get radio button label from different radio button groups and append to division

There are 3 radio button groups for user to select.
Division '#targetOption' will get whenever the user checked radio button label to be displayed. Below my code which is able to get the first checked radio button.
Example if user click on the first radio button group, #targetOption will show A. Then if user click on the second radio button group, #targetOption will show A B.
<div id="options">
<div class="form-group required">
<div class="radio">
<label> <input type="radio" value="a">
A
</label>
</div>
<div class="radio">
<label> <input type="radio" value="b">
B
</label>
</div>
<div class="radio">
<label> <input type="radio" value="c">
C
</label>
</div>
</div>
<div class="form-group required">
<div class="radio">
<label> <input type="radio" value="d">
D
</label>
</div>
<div class="radio">
<label> <input type="radio" value="e">
E
</label>
</div>
<div class="radio">
<label> <input type="radio" value="f">
F
</label>
</div>
</div>
<div class="form-group required">
<div class="radio">
<label> <input type="radio" value="g">
G
</label>
</div>
<div class="radio">
<label> <input type="radio" value="h">
H
</label>
</div>
<div class="radio">
<label> <input type="radio" value="i">
I
</label>
</div>
</div>
</div>
<div id="targetID"></div>
$('#options').click(function() {
$('#targetID').html('');
$("input[type='radio']:checked").each(function() {
var optiontext = $(this).parent().text();
console.log(optiontext);
$('#targetID').html(optiontext);
});
});
$('#targetID').html(optiontext); set #targetID to the current radio value in the loop instead of appending it to #targetID.
Use $('#targetID').html($('#targetID').html() + optiontext); instead.
Or you can create an array to store the checked values and update #targetID later

Show and hide inputs depending on radio button angular 2

I would like to show and hide addtional input fields depending on a radio button value.
Radio
<div class="form-group">
<label>Was möchten Sie bewerten?</label>
<div class="radio">
<label><input type="radio" [(ngModel)]="type" name="type" value="ETW">Eigentumswohnung</label>
</div>
<div class="radio">
<label><input type="radio" [(ngModel)]="type" name="type" value= "EFH">Einfamilienhaus</label>
</div>
<div class="radio">
<label><input type="radio" [(ngModel)]="type" name="type" value="ZFH">Mehrfamilienhaus</label>
</div>
</div
This form/elemnt I would like to show if the value of type is "EFH" or "ZFH"
<div *ngIf="" class="form-group">
As your radio buttons are binded to some property using ngModel, you can use your radio buttons model property to check (show/hide) the div's.
Example :
<div *ngIf="type == 'EFG' || type == 'ZFH'" class="form-group">

ng-link is not working with radio buttons

I am trying to change content using radio buttons. content changes when click on radio button but radio is not checked it stays empty, i know something is wrong, can someone suggest me a solution.
<div class="radio-custom radio-default radio-inline">
<input type="radio" ng-link="['OneWayFlight']" id="oneWayFlight" name="FlightType" />
<label for="oneWayFlight">One way</label>
</div>
<div class="radio-custom radio-default radio-inline">
<input type="radio" ng-link="['ReturnTripFlight']" id="roundTripFlight" name="FlightType"/>
<label for="roundTripFlight">Round Trip</label>
You can use ng-model instead of ng-link.
<div class="radio-custom radio-default radio-inline">
<input type="radio" ng-model="flightType" value="Round Trip">Round trip<br/>
<input type="radio" ng-model="flightType" value="One Way">One Way<br/>
<tt>Flight Type = {{flightType | json}}</tt><br/>
</div>

How to make selecting a radio button required before submitting

I have a series of questions which have radio answer choices. I can't figure out how to use AngularJS validation to require the user to select one before clicking "Next". Below is my code:
EDIT: Please note that clicking "Next" gets the next question node from the controller depending on what choice was made. It's basically a dynamic questionnaire.
<form novalidate>
<div class="radio" ng-repeat="answer in node.Answers">
<input type="radio" name="answerGroup" ng-model="$parent.selectedAnswer"
value="{{answer.BranchId}},{{node.LeafId}},{{answer.Id}}"/> {{answer.Text}}
</div>
<div>
<input type="button" ng-click="previous()" value="Previous"/>
<input type="button" ng-click="next(selectedAnswer)" value="Next"/>
</div>
</form>
EDIT: Here is a working fiddle
First give the form a name so that you can refer to it:
<form name="myForm" novalidate>
Next add the required attribute to the radio button:
<input type="radio" name="answerGroup" required
ng-model="$parent.selectedAnswer"
value="{{answer.BranchId}},{{node.LeafId}},{{answer.Id}}"/>
Then use ng-disabled to bind your next button's disabled property to the validity of the radio button:
<input type="button" ng-click="next(selectedAnswer)" value="Next"
ng-disabled="myform.answerGroup.$invalid" />
Look below, using ng-show to display an error message should neither radio button be clicked.
<label for="dateofbirth">
<span>Are you the primary cardholder?</span>
</label>
<ul>
<li>
<label for="yes">
<input type="radio" id="yes" name="cardholder" ng-model="user.cardholder" value="yes" required/>
Yes
</label>
</li>
<li>
<label for="no">
<input type="radio" id="no" name="cardholder" ng-model="user.cardholder" value="no" required/>
No
</label>
</li>
</ul>
</fieldset>
<span class="error" ng-show="myForm.cardholder.$error.required && submitted == true"><i class="fa fa-exclamation-circle"></i>Please select an answer.</span>

Categories

Resources