Getting the Value of Radio Button - html

Edit:
I am getting undefined when I tried to submit the ng-model of the radio.
Sample code in my submit button:
ng-click="addSubscriber(plan)"
Tried to console.log(plan) inside the addSubscriber() but getting undefined.
I am having headache because of the problem of getting the selected value of radio button. The value is showing and it is rendering radio depends on my data. But ng-model doesn't work, I can't get the value of selected radio.
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div ng-repeat="plan in plans" class="radio">
<label><input type="radio" ng-model="plan.plan" value="{{plan.plan_code}}" name="plan.plan_name">{{plan.plan_name | capitalize}}
</label>
</div>
</div>
<pre>{{plan.plan}}</pre>
</div>
No value is showing in <pre> tag. I searched and tried some posted answers in other topics but its not working.

Update:
Another problem was that the ng-model inside the ng-repeat was unable to update the controller variable, this is due to that fact that.
ng-repeat directive creates a new scope.
Thus we need to change the ng-model from chosenPlan to $parent.chosenPlan, which will update the parent scope instead.
In the above scenario, the input radio buttons need to be set under a common name for them to act as group of radio buttons. The value of the radio button also needs to be set to a common variable which can be used in the submit function.
<label>
<input type="radio" ng-model="$parent.chosenPlan" ng-value="plan.plan_code" name="test"/>
{{plan.plan_name | capitalize}}
</label>
In the above form we can see that the ng-model is set to a common variable. The group name name attribute is set to a common name.
Please find below a working model of the same.
JSFiddle Demo
Old Answer:
The <pre> tag lies outside the ng-repeat move it inside, change the value to ng-value as shown below. Name for it to have the proper value should be name="{{plan.plan_name}}". Let me know if you face any issues.
<div ng-controller='MyController'>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div ng-repeat="plan in plans" class="radio">
<label>
<input type="radio" ng-model="plan.plan" ng-value="plan.plan_code" name="{{plan.plan_name}}"/>{{plan.plan_name | capitalize}}
</label>
<pre>{{plan.plan}}</pre>
</div>
</div>
</div>
</div>
Mocked up the data and providing an example for reference:
JSFiddle Demo

Your <pre>{{plan.plan}}</pre> is out of the scope of the ng-repeat so plan.plan is unrecognized. If you would like to show a value for each radio button, you can try it like that -
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div ng-repeat="plan in plans" class="radio">
<label><input type="radio" ng-change="radioChanged()" ng-model="plan.plan" value="{{plan.plan_code}}" name="plan.plan_name">{{plan.plan_name}}
</label>
<pre>{{plan.plan}}</pre>
</div>
</div>
I have also added a 'ng-change' function, so you can try to put some debugger inside that function and watch the changed value inside your controller.

Related

How to you have a pre-set value in a textbox in thymeleaf?

I have the below code snippet of a text box where one enters a mobile number. How do I have a pre-set value that a user can edit from?
For example:
The country code is already set and visible to the user, the user starts by entering their network provider number.
<div class="row">
<div class="col-md-6">
<div th:class="transfer-label"> Mobile Number
</div>
</div>
<div class="col-md-6">
<div class="form-group transfer-field">
<input th:id="msdn"
class="custom-input-normal rounded-input hollow-input-light"
type="text" name="msdn"/>
</div>
</div>
</div>
you can populate input tag with value using th:value attribute.
<input th:value="${variable}"> will render <input value="variablecontent">

Show clarity radio buttons based on a condition in Angular

I have a div which has a couple of clr radio buttons wrapped in a .I want to generate the radioButtons based on a boolean value. Following is the code.
<div class="clr-col-4">
<clr-radio-container>
<label class="display-label">Radio Div</label>
<clr-radio-wrapper *ngFor="let col of cols">
<input type="radio" clrRadio />
<label class="display-label" {{col.headerName}}</label>
</clr-radio-wrapper>
</clr-radio-container>
</div>
I have a variable named enableButton. If the enableButton is true, I want to show the radio button, else I don't want to. Could you please help me how to do this?
You can use the ngIf directive. It is an essential thing to have in mind when creating Angular templates.
<div class="clr-col-4">
<clr-radio-container *ngIf="enableButton">
<label class="display-label">Radio Div</label>
<clr-radio-wrapper *ngFor="let col of cols">
<input type="radio" clrRadio />
<label class="display-label" {{col.headerName}}</label>
</clr-radio-wrapper>
</clr-radio-container>
</div>
I suggest you read the docs to familiarise more with the directive and its other conditional variations like then, else, etc.

Is there a way to create a labeled checkbox as made in w3school example for textbox?

Looking the w3Schools Bootstrap's Input Groups examples there are Input Groups Labels that seems that wrap the input box
(here).
Is there any way to do this for checkbox too?
EDIT
The labels have to be inside that grey box attached to the input box (textbox in example case, checkbox in the case i want)
They don't wrap the input field since the closing tag comes right after the opening tag, but you can use label on a checkbox as well as described here in the offical bootsrap docs for forms:
Is it something like this you had in mind?
<div class="container mt-3">
<h2>Checkbox Group Labels</h2>
<form>
<label class="form-check-label" for="exampleCheck1">Check me out</label>
<div class="input-group mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
</div>
</div>
</form>
</div>

HTML date picker overlaps date input

When I use the following code, the date input fields are cut off and overlapped by the native HTML date picker:
<div class="form-check form-check-inline">
<div class="input-group ml-2">
<div class="input-group-prepend">
<label class="input-group-text" for="date_from">von</label>
</div>
<input th:placeholder="#{date.format}" th:field="*{from}" type="date" max="9999-12-31" min="1000-01-01"
class="form-control" id="date_from">
</div>
<div class="input-group ml-2">
<div class="input-group-prepend">
<label class="input-group-text" for="date_to">bis</label>
</div>
<input th:placeholder="#{date.format}" th:field="*{to}" type="date" max="9999-12-31" min="1000-01-01"
class="form-control" id="date_to">
</div>
</div>
This is how it looks like in the browser:
When hovering over the input, one can clearly see that it's the native HTML date picker that overlaps the input fields:
So my question is: How can I solve this problem?
You could use one of two options:
Specify the width of the input box to a value that allows the whole date to be clearly visible.style="width:(insert value here)"
Specify a smaller font-size, this will also allow the date to be seen clearly. style="font-size:(insert value here)"
I deal with this everyday using the two steps Alex Morrison explained combined, also, don't forget that you can do both steps multiple times in different media queries to make it more responsive.
Instead of having to manually add the width every time you have a date input, you could either create a custom class, or target each date in css and do something along the lines of input[type=date]{width:(your width here);}.

html, css: three state button

I would like to create three state button in Angular2 using css- yes/none/no without using jQuery. I found this link in some answer on stackoverflow. The point is, that I need to put a few buttons next to each other, and each of them should be independent- every chosen value should be send to component. I've tried to use label implicitly (remove inputs ids), but it doesn't work. Please see full code: fiddle.
<div class="wrapper">
<div class="toggle_radio">
<label for="first_toggle">First Button <input type="radio" class="first_toggle" name="toggle_option"></label>
<label for="second_toggle">Second Button<input type="radio" checked class="second_toggle" name="toggle_option"></label>
<label for="third_toggle">Third Button <input type="radio" class="third_toggle" name="toggle_option"></label>
<div class="toggle_option_slider">
</div>
</div>
</div>