How I can set a checked attribute for an input in my form, using Thymeleaf?
Here is my code, which currently doesn't work:
<label th:each="cat : ${categories}">
<input type="checkbox" value=""
th:value="${cat.id}"
th:text="${cat.description}"
th:checked="${recipe.getCategories().contains(cat) ? true : false}"
/>
</label>
As stated in comments, the problem may be from somewhere else but try this and see if it helps:
th:checked="${recipe.getCategories().contains(cat)}"
Related
I have an input field where I want conditionally apply CSS class.
For example if firstName == undefined apply CSS class ng-dirty. So, I tried
<input required [(ngModel)]="customer.firstName" name="firstName"
type="text" minlength="2" maxlength="50" ng-class="{ng-dirty : customer.firstName === undefined}">
that however doesn't work.
If you are using Angular 2/ Angular 4 the below is the conditional syntax for NgClass
<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>
and if You are using angularJs(i.e. angular 1.x) use below
<some-element ng-class="{'fist' : true, 'second': true}">...</some-element>
Official Docs for Angular4 [NgClass]
Official DOcs for AngularJS ng-class
hope this helps :)
You could try this:
<input required [(ngModel)]="customer.firstName" name="firstName"
type="text" minlength="2" maxlength="50" [class]="(customer.firstName == undefined) ? 'dirty' : ''" >
But I don't think customer.firstName will ever be undefined, since you defined it when you put it in the model.
Js object notation can't have - dashes all you need is a quote to make it happen
<input required [(ngModel)]="customer.firstName" name="firstName"
type="text" minlength="2" maxlength="50" ng-class="{'ng-dirty' : customer.firstName === undefined}">
I'm trying to load a form with an object that has a enum property, seems that everything is working correctly, but when I try to apply a class I get an error. I can see in the HTML code that checked property is been apply correctly, however I need to apply an specific class to the checked element and in the following line is the one that I have the problem.
th:classappend="${'__${currency}__' == '__${reference.currency}__' ? 'active'}"
The complete element looks like this
<div class="btn-group" data-toggle="buttons">
<label th:each="currency : ${T(entity.CurrencyEnum).values()}"
th:for="${#ids.next('currency')}" class="btn btn-default" th:classappend="${'__${currency}__' == '__${reference.currency}__' ? 'active'}">
<input type="radio" th:name="currency" th:field="*{currency}"
th:text="${currency}" th:value="${currency}" />
</label>
</div>
Thanks in advance...
--- UPDATE ---
Here is a sample code after solving the issue. The problem was where I place the final } please be carefull with this detail.
<div th:fragment="currency (selected)">
<label
th:each="currency : ${T(CurrencyEnum).values()}"
th:for="${#ids.next('currency')}" class="btn btn-default"
th:classappend="${currency == selected} ? 'active'"> <input type="radio"
th:name="currency" th:field="*{currency}" th:text="${currency}" th:value="${currency}" />
</label>
</div>
Assuming that reference is a variable defined somewhere and it is visible in this contex, you can try with this:
th:classappend="${currency} eq ${reference.currency} ? 'active'"
I'm using handlebars in a backbone.js rails app, and I have a Boolean field I'm populating with a checkbox.
When I load the edit page, the form is populated with the contents from the server JSON something like
{id:3,user:'test',checkbox:1}
now in my handlebar form, I want to show that the checkbox is 1.
< input type="checkbox" name="checkbox" value="1" {{#if checkbox}} {{bindAttr checkbox checked="isSelected"}}{{/if}} >
but this isn't returning the checked checkbox. I'd really like to just be able to say if checkbox==1, but I don't see how I can do that with handlebars.
Anysuggestions??
What you would usually do, is using a Boolean in the 'model'.
{
isChecked: true
}
and then
<input type="checkbox" {{bindAttr checked="isChecked"}}>
If the Boolean is true, it will render the checked property, and if the Boolean is false, it would omit the property. So if isChecked is true, then Handlebars would output
<input type="checkbox" checked>
and if isChecked were false, we would get
<input type="checkbox">
Which is what we want!
I also wrote a helper to do this. It doesn't use backbone.js, so may be an alternative for some:
Handlebars.registerHelper('checked', function(currentValue) {
return currentValue == '1' ? ' checked="checked"' : '';
});
Usage example:
<input type="checkbox" name="cbxExample" id="cbxExample" {{checked cbxExample}}/>
Would tick a checkbox if the supplied JSON was:
{"cbxExample" : "1"}
Resulting in:
<input type="checkbox" name="cbxExample" id="cbxExample" checked="checked" />
[my first post - hope that's helpful!]
It seems this ought to be dead simple, but I'm stuck. I've written some asp.net code that outputs a pair of radio buttons:
<p>
<label for='chkYapper'>Yapper</label>
<input type='radio' name='yapper' id='chkYapper' value='yapper' checked='<%=gblYapperChecked %>' />
<br />
<label for='chkNonYapper'>non-Yapper</label>
<input type='radio' name='Yapper' id='chkNonYapper' value='nonYapper' checked='<%=gblNonYapperChecked %>' />
if (registrationUser.isYapper == 1)
{
gblYapperChecked = "checked";
gblNonYapperChecked = "";
}
else
{
gblYapperChecked = "";
gblNonYapperChecked = "checked";
}
As expected, I get two radio buttons, "Yapper" and "Non-Yapper". However, even when I step thru my code and see that gblYapperChecked is "checked" and gblNonYapperChecked is "", Non-Yapper is always selected by default in the web browser.
What am I doing wrong?
UpdateHere is the HTML code as it actually appears in the browser. "Yapper" should be selected, but "Non-Yapper" appears selected instead.
<p>
<label for='chkYapper'>Yapper</label>
<input type='radio' name='yapper' id='chkYapper' value='yapper' checked='checked' />
<br />
<label for='chkNonYapper'>non-Yapper</label>
<input type='radio' name='yapper' id='chkNonYapper' value='nonYapper' checked='' />
Note that the HTML "checked" attribute is generally determined by being present or not present. See http://www.w3.org/TR/html401/interact/forms.html#adef-checked for the spec.
In particular what this means is that if you want it to be checked you cna have checked, checked=true, checked=checked and so on. So what you want is to not have the checked attribute at all if you don't want the checkbox selected.
I would advise structure such as:
<input type='radio' name='Yapper' id='chkNonYapper' value='nonYapper' <%=registrationUser.isYapper?"":"checked='checked'" %> />
This should eliminate your checked attribute entirely dependant on your isYapper boolean.
The "checked" attribute is weird, it has no value. If a radio button is checked, include the "checked" attribute by itself in the tag. If unchecked, don't do anything. See here:
http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_RADIO.html
Are you setting dblYapperChecked before or after the control is created? Personally, I'd run the radio buttons on the server side and set the checked value on the control directly, but your method should work if the values are set soon enough (try initializing them to the expected values and see if that makes a difference...)
I have the following in my jspx file:
<jsp:scriplet>
int myvar = 2;
</jsp:scriptlet>
How can I put variable myvar into a textbox (id=myinput) value using JSTL or scriptlet (I can do this using session variable)
<input type="text" id="myinput" value="...the value of myvar..."/>
Thanks
<input type="text" id="myinput" value="<%=myvar%>"/>
Just a little typo in previous answer. Please use like this
<input type="text" id="myinput" value="<%=myvar%>"/>