I'm new to Angular 5 and I'm having radio button like component in my app as follows.
<div class="btn-group btn-group-toggle" ngbRadioGroup name="radioBasic" [(ngModel)]="vehicles">
<label ngbButtonLabel class="btn btn-secondary active">
<input ngbButton type="radio" name="mode" [value]="true" > Toyota
</label>
<label ngbButtonLabel class="btn btn-secondary">
<input ngbButton type="radio" name="mode" [value]="false"> Nissan
</label>
</div>
I also have a separate div as follows. I want to show that div only a user selects Nissan button. I try to use ngif. But failed. Can someone please help me.
<div class="col-sm-4">
<div class="form-group">
<label class="control-label">Vehicle Type</label>
<ng-select
[items]="vehicleTypes"
bindLabel="name"
bindValue="id"
[(ngModel)]="selectedVehicleTypesId"
>
</ng-select>
</div>
</div>
Here is what you can try.
<label class="btn btn-secondary active">
<input type="radio" [(ngModel)]="show" name="bn" [value]="true"> Show
</label>
<label class="btn btn-secondary active">
<input type="radio" [(ngModel)]="show" name="bn" [value]='false'> ShowSecond </label>
<div *ngIf="show">
This will be togled for 1st Radio
</div>
<div *ngIf="!show">
This will be togled for 2nd Radio
</div>
Declare a property show in you component as boolean and initialize it to false.
show: boolean = false;
Here is a Demo of the same.
You can now change things according to your own code and structure.
Hope this helps
Related
<div class="form-row">
<div class="column">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-primary active" for="visualContagi">
<input type="radio" name="options" id="visualContagi" checked> Contagi
</label>
<label class="btn btn-primary" for="visualGuariti">
<input type="radio" name="options" id="visualGuariti" checked> Guariti
</label>
<label class="btn btn-primary" for="visualDecessi">
<input type="radio" name="options" id="visualDecessi" checked> Decessi
</label>
<label class="btn btn-primary" for="visualRicoveri">
<input type="radio" name="options" id="visualRicoveri" checked> Ricoveri
</label>
<label class="btn btn-primary" for="visualTotPos">
<input type="radio" name="options" id="visualTotPos" checked> Totale Positivi
</label>
</div>
</div>
</div>
Hi, I have this radio button group as part of a form. The problem is that the buttons all get the "active" class applied to them when I load the webpage. I haven't written any javascript, it's just bootstrap, and this CSS applied to remove the outline when I click on the buttons:
.btn-primary.focus, .btn-primary:focus {
box-shadow: none;
}
I'm using the latest version of Firefox on macOS.
What I am noticing is that each of your <input type="radio" elements have the checked attribute attatched to them. If you remove that, then it should be fixed. I hope this helps!
I have two bootstrap toggle buttons, but when I click one of them, the other is getting affected. Can you please tell me what is wrong in my code ?
<div class="row p-0 m-0">
<!-- all day event toggle -->
<div class="col-md-3">
<label for="allDayEvent">All Day Event:</label><br>
<div class="btn-group btn-group-toggle" id="AllDayEventButton" data-toggle="buttons" disable="false">
<label class="btn btn-secondary" id="allDayEventYesLbl" >
<input type="radio" name="options" id="allDayEventYes" autocomplete="off">
Yes
</label>
<label class="btn btn-secondary active " id="allDayEventNoLbl" >
<input type="radio" name="options" id="allDayEventNo" autocomplete="off">
No
</label>
</div>
</div>
<!-- all day event toggle END -->
<!-- Recurring event toggle -->
<div class="col-md-3">
<label for="recurringEvent">Recurring Event:</label><br>
<div class="btn-group btn-group-toggle" id="RecurringButton" data-toggle="buttons">
<label class="btn btn-secondary" id="recurringEventYesLbl">
<input type="radio" name="options" value="Y" id="recurringYes" autocomplete="off">
Yes
</label>
<label class="btn btn-secondary active" id="recurringEventNoLbl">
<input type="radio" name="options" value="N" id="recurringNo" autocomplete="off" checked>
No
</label>
</div>
</div>
<!-- recurring event toggle END -->
</div>
There is very simple mistake you might have overlooked. Both the inputs in both radio buttons have the same name. Inputs in same radio button should have same name, but in different groups should have different names. Just changing the names in the second group will fix it. Here is a fixed version!
<div class="row p-0 m-0">
<!-- all day event toggle -->
<div class="col-md-3">
<label for="allDayEvent">All Day Event:</label><br>
<div class="btn-group btn-group-toggle" id="AllDayEventButton" data-toggle="buttons" disable="false">
<label class="btn btn-secondary" id="allDayEventYesLbl" >
<input type="radio" name="options" id="allDayEventYes" autocomplete="off">
Yes
</label>
<label class="btn btn-secondary active " id="allDayEventNoLbl" >
<input type="radio" name="options" id="allDayEventNo" autocomplete="off">
No
</label>
</div>
</div>
<!-- all day event toggle END -->
<!-- Recurring event toggle -->
<div class="col-md-3">
<label for="recurringEvent">Recurring Event:</label><br>
<div class="btn-group btn-group-toggle" id="RecurringButton" data-toggle="buttons">
<label class="btn btn-secondary" id="recurringEventYesLbl">
<input type="radio" name="optionsTwo" value="Y" id="recurringYes" autocomplete="off">
Yes
</label>
<label class="btn btn-secondary active" id="recurringEventNoLbl">
<input type="radio" name="optionsTwo" value="N" id="recurringNo" autocomplete="off" checked>
No
</label>
</div>
</div>
<!-- recurring event toggle END -->
</div>
I have a modal form in Twig to add a new questions to a ticket system.
In this form I can add more options(answers) to the question and they will be in array.
And I have radio button near every answer filled, I want this button values to be in the same array for the answers.
I'm new to twig so I tried to use set but I didn't knew how exactly it should be.
Here is my code :
<div class="form-inline" id="showOptions" name"test">
<input class="form-control" name="answers[]" type="text" placeholder="Answer here"/>
<button onclick="javascript:void(0);" class="add_button btn btn-success btn-add" type="button">
<span class="glyphicon glyphicon-plus"> </span>
</button>
<div class="radio">
<fieldset id="group1">
<label>bla bla?</label>
<label class="radio" ><input name="decision[]" type="radio" value"1">Ja</label>
<label class="radio" ><input name="decision[]" type="radio" value"0">Nein</label>
</fieldset>
</div>
</div>
and it would looks like this
create the field name like this.
<div class="form-inline" id="showOptions" name"test">
<input class="form-control" name="answers[$i][1]" type="text" placeholder="Answer here"/>
<button onclick="javascript:void(0);" class="add_button btn btn-success btn-add" type="button">
<span class="glyphicon glyphicon-plus"> </span>
</button>
<div class="radio">
<fieldset id="group1">
<label>bla bla?</label>
<label class="radio" ><input name="answers[$i][2]" type="radio" value"1">Ja</label>
<label class="radio" ><input name="answers[$i][3]" type="radio" value"0">Nein</label>
</fieldset>
</div>
</div>
Where $i will increase by 1 when new row is added.
In case #1 works, in case #2 it do not works. Check the code bellow:
<div class="container">
<div class="row">
<h1>Radio Group #1</h1>
<label class="radio-inline">
<input name="radioGroup" id="radio1" value="option1" type="radio"> 1
</label>
<label class="radio-inline">
<input name="radioGroup" id="radio2" value="option2" checked="checked" type="radio"> 2
</label>
<label class="radio-inline">
<input name="radioGroup" id="radio3" value="option3" type="radio"> 3
</label>
</div>
<div class="row">
<h1>Radio Group #2</h1>
<label for="year" class="control-label input-group">Year</label>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input name="year" value="2011" type="radio">2011
</label>
<label class="btn btn-default">
<input name="year" value="2012" type="radio">2012
</label>
<label class="btn btn-default">
<input name="year" value="2013" checked="checked" type="radio">2013
</label>
</div>
</div>
</div>
You can see it in action here: http://bootply.com/84165
Assuming you want a default button checked.
<div class="row">
<h1>Radio Group #2</h1>
<label for="year" class="control-label input-group">Year</label>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="radio" name="year" value="2011">2011
</label>
<label class="btn btn-default">
<input type="radio" name="year" value="2012">2012
</label>
<label class="btn btn-default active">
<input type="radio" name="year" value="2013" checked="">2013
</label>
</div>
</div>
Add the active class to the button (label tag) you want defaulted and checked="" to its input tag so it gets submitted in the form by default.
A javascript fix to apply the 'active' class to all labels that are parents of checked inputs:
$(':input:checked').parent('.btn').addClass('active');
insert right after
$('.btn').button();
You have to use active in the label to make it work as mentioned above. But you can use checked="checked" and it will work too. It's not necessary but it's more legible and makes more sense as it is more html format compliance.
Use active class with label to make it auto select and use checked="" .
<label class="btn btn-primary active" value="regular" style="width:47%">
<input type="radio" name="service" checked="" > Regular </label>
<label class="btn btn-primary " value="express" style="width:46%">
<input type="radio" name="service"> Express </label>
In case you want to use bootstrap radio to check one of them depends on the result of your checked var in the .ts file.
component.html
<h1>Radio Group #1</h1>
<div class="btn-group btn-group-toggle" data-toggle="buttons" >
<label [ngClass]="checked ? 'active' : ''" class="btn btn-outline-secondary">
<input name="radio" id="radio1" value="option1" type="radio"> TRUE
</label>
<label [ngClass]="!checked ? 'active' : ''" class="btn btn-outline-secondary">
<input name="radio" id="radio2" value="option2" type="radio"> FALSE
</label>
</div>
component.ts file
#Component({
selector: '',
templateUrl: './.component.html',
styleUrls: ['./.component.css']
})
export class radioComponent implements OnInit {
checked = true;
}
Preselecting a radio button in twitter's bootstrap without using jquery.
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" id="athletebutton" value="1" checked> Athlete </label>
<label class="btn btn-primary">
<input type="radio" name="options" id="coachbutton"> Coach </label>
</div>
Mark the label class with active
See screenshots & video here:
Video:http://d.pr/v/b3Gm
Screenshot: