Knockout Binding Messes up Radio Button Formatting - html

As a prototype, I hard coded a radio button list, just to work out the CSS, etc. When I converted it to be generated from a JSON list using Knockout, however, the formatting went crazy. Here is the original HTML:
<input id="pcp1" type="radio" name="pcp" /><label for="pcp1"><span></span>One</label><br />
<input id="pcp2" type="radio" name="pcp" /><label for="pcp2"><span></span>Two</label><br />
<input id="pcp3" type="radio" name="pcp" /><label for="pcp3"><span></span>Three</label><br />
Here is the Knockout version. It gets the data from the ajax call correctly (3 items with the correct text):
<div data-bind="foreach: PrimaryCareProviders">
<input data-bind="attr: { id: Id }" type="radio" name="pcp" /><label data-bind="attr: { for: Id }"><span></span><span data-bind="text:Name"></span></label><br/>
</div>
What's going on here?
Thanks,
Jay

This looks fine. You might want to inspect your HTML elements in your debugger. Also, if you're using some kind of toolkit to style things, that would be a problem.
v = {
PrimaryCareProviders: [{
Id: 1, Name: 'One'
}, {
Id: 2, Name: 'Two'
}, {
Id: 3, Name: 'Three'
}]
};
ko.applyBindings(v);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div data-bind="foreach: PrimaryCareProviders">
<input data-bind="attr: { id: Id }" type="radio" name="pcp" />
<label data-bind="attr: { for: Id }">
<span></span>
<span data-bind="text:Name"></span>
</label>
<br/>
</div>

The problem is that I added a second span to do the Knockout binding of the Name field. I assumed I needed to use an HTML element to bind to. Turns out you don't need to bind to an HTML element in Knockout. You can use "containerless syntax."
So, I replaced my second <span> with this, and that fixed it:
<!--ko text:Name--><!--/ko-->

Related

Freemarker how to select checkboxes on UI send by backend?

Have following data passed from backend:
ALL_CONFIGS: {callDigitalIo=true, controllerId=1, numberPlatesHashSalt=..., numberPlatesShouldBeHashed=false, parkingStatusShouldBeChecked=true}
Where boolean params should be displayed like input element with type=checkbox.
Here is how I handle it on UI page (.ftl):
<div>
<label class="col-form-label">
Parking Status Should Be Checked:
<input type="checkbox" class="form-check-input ml-2"
id="parkingStatusShouldBeChecked" name="parkingStatusShouldBeChecked"
<#if parkingStatusShouldBeChecked??>checked="checked"</#if>
/>
</label>
</div>
However, checkboxes are all selected:
Number Plates Should be Hashed should not be selected.
How to select only true variables?
After huge research I found solution for this issue:
<div>
<label class="col-form-label">
Parking Status Should Be Checked:
<input type="checkbox" class="form-check-input ml-2"
id="parkingStatusShouldBeChecked"
<#if parkingStatusShouldBeChecked == "true">checked</#if>
/>
</label>
</div>
It works fine.
Assuming that parkingStatusShouldBeChecked is a boolean, this way works too:
<input type="checkbox" class="form-check-input ml-2"
id="parkingStatusShouldBeChecked" name="parkingStatusShouldBeChecked"
${parkingStatusShouldBeChecked?string('checked','')} />
The first parameter is the "true" expression, so FreeMarker prints checked. Otherwise it prints just an empty string.
See https://freemarker.apache.org/docs/ref_builtins_boolean.html#ref_builtin_string_for_boolean

How do I give radio-buttons in a form a custom look?

Code:
<label class="form__label" for="urgency">Urgency</label>
<input class="form__input no-input-animation" id="urgency" name="urgency" type="radio" required>
<div class="urgency-text critical">Critical</div>
<div class="urgency-text high">High</div>
<div class="urgency-text medium">Medium</div>
<div class="urgency-text low">Low</div>
This shows one radio button with each urgency div as one option collectively. I can understand why.
I would like my buttons to have no radio checkbox and each of the divs be its own option, with a value attribute.
EXAMPLE:
Choose Status:
[ Available ] [ Idle ] [ Do Not Disturb ]
Each item in square brackets [] is clickable and has it's own value attribute, e.g. available
How do I make this work inside a form?
Thanks.
I will suggest you to refer to this as an example. I believe this is exactly what you're looking for.
<div class="container">
<div class="segmented-control" style="width: 100%; color: #5FBAAC">
<input type="radio" name="sc-1-1" id="sc-1-1-1">
<input type="radio" name="sc-1-1" id="sc-1-1-2">
<input type="radio" name="sc-1-1" id="sc-1-1-3" checked>
<label for="sc-1-1-1" data-value="High">High</label>
<label for="sc-1-1-2" data-value="Medium">Medium</label>
<label for="sc-1-1-3" data-value="Low">Low</label>
</div>
CodePen Example <-- Is what youre looking for.
Visit this blog for more information.

vue components and tabindex, how do you do it?

I have a custom view component that renders as three select lists, "Year", "Make" and "Model" and it's working well for me.
My problem lies when it's dynamically placed on a parent form... The tab order is out of whack and my research seems to indicate that is normal without a tabindex set.
Let's say I have something like this:
<input type="text" v-model="name" tabindex="1">
<my-widget v-if="someCondition"> </my-widget>
<input type="text" v-model="shop" tabindex="5">
How do I tell myWidget to set the tabindex for the selects inside my widget to 2,3,4?
Ideally I'd like to be able to use two of these widgets on the same page so hardcoding wont work.
Does anyone have any thoughts on the best way to assign tabindex inside a component?
You could have the tabindex as a prop on your my-widget component so that it can be dynamic. For example
my-widget component
<template>
<div>
<input type="text" :tabindex="tabindex"/>
<input type="text" :tabindex="tabindex + 1"/>
</div>
</template>
<script>
export default {
props: {
tabindex: {
type: Number,
required: false,
default: 1
}
}
</script>
so then your code will look like
<input type="text" v-model="name" tabindex="1">
<my-widget v-if="someCondition"
:tabindex="2"
> </my-widget>
<input type="text" v-model="shop" tabindex="5">
and if you add another one
<my-widget v-if="someCondition"
:tabindex="6"
> </my-widget>

HTML Label "For" Value

I have a label tag that I am trying to link to an input type checkbox tag. I have multiple labels and multiple checkbox inputs, and they all have the same id and the same name, but different values. Can someone instruct me as how to construct a label that links to a value rather than an id? So this:
<label for="8994"></label>
Would link to:
<input id="member_ids_" name="member_ids[]" type="checkbox" value="8994">
Or is this not possible?
The label's for attribute must match the ID of the <input> element it refers to. In your case it would be something like:
<label for="member_id_8994"></label>
<input id="member_id_8994" name="member_ids[]" type="checkbox" value="8994">
The 'for' for the form element must match with the ID of the same form element.
<label for="id_1"></label>
<input id="id_1" name="member_ids[1]" type="checkbox" value="8994">
<label for="id_2"></label>
<input id="id_2" name="member_ids[2]" type="checkbox" value="8994">
<label for="id_3"></label>
<input id="id_3" name="member_ids[3]" type="checkbox" value="8994">
<label for="id_3"></label>
<input id="id_3" name="member_ids[4]" type="checkbox" value="8994">
Your DOM elements must have different IDs.
Even if each ID is just set to whatever that value is... ...or whatever.
They can not have the same ID.
Once you've got that out of the way, setting for on a label becomes really simple.
I doubt if that is possible. Label's for are tied to the id attribute of inputs. One way to do achieve your objective maybe through javascript, knockout's declarative bindings for instance.
check it our here: http://knockoutjs.com/documentation/introduction.html
Something along this line:
<label data-bind="click: myInput"></label>
<input type="checkbox" id="hello">
<script type="text/javascript">
var myInput= {
//implement the function to bind the label to the input#hello here
}
};
</script>
http://knockoutjs.com/documentation/click-binding.html
A jQuery solution that probably doesn't work.
$(function() {
ModifyLabels({target: $('input')});
$('input').change(ModifyLabels);
});
function ModifyLabels(eventObject) {
var inputs = $(eventObject.target);
input.each(function(i, input) {
$('label').each(function(i, label) {
if ($(label).attr('for') == $(input).val()) {
$(input).attr('id', $(input).val());
}
});
});
}

How can I prevent a user from selecting multiple checkboxes in HTML?

How can I prevent a user from selecting multiple checkboxes in HTML?
you should change it to radio button instead of check box!
<input type="radio" name="group1" id="item1" value="Milk">
<label for="item1">Milk</label>
<br/>
<input type="radio" name="group1" id="item2" value="Butter" checked>
<label for="item2">Butter</label>
<br/>
<input type="radio" name="group1" id="item3" value="Cheese">
<label for="item13">Cheese</label>
I had a use case where I needed use checkboxes--which, unlike radio buttons, allows a user to UNcheck... Here's an example of something I pieced together from another stackoverflow user:
<head>
<meta charset=utf-8 />
<title>and-or checkboxes</title>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
<label for="checkbox1"><input type="checkbox" id="checkbox1" name="checkbox1" value="and" </label><span id="span_and">checkbox1</span><br>
<label for="checkbox2"> <input type="checkbox" id="checkbox2" name="checkbox2" value="or" </label><span id="span_or">checkbox2</span>
<script>
$(document).ready(function(){
$('#checkbox1').click(function() {
var checkedBox = $(this).attr("checked");
if (checkedBox === true) {
$("#checkbox2").attr('checked', false);
} else {
$("#checkbox2").removeAttr('checked');
}
});
});
$(document).ready(function(){
$('#checkbox2').click(function() {
var checkedBox = $(this).attr("checked");
if (checkedBox === true) {
$("#checkbox1").attr('checked', false);
} else {
$("#checkbox1").removeAttr('checked');
}
});
});
</script>
</body>
With a little work, you can combine the either/or two scripts into a single script. You probably don't need this now but I wanted to post it because it was very useful to me.
If you want that only one checkbox get selected at a time then its better to use radiobutton instead.
If you mean that you don't want multiple checkboxes from a same "logical group" to be checked at one time, you should use radio buttons instead of checkboxes.
<form>
<input type="radio" name="aGroup" value="choice1" /> Choice #1<br />
<input type="radio" name="aGroup" value="choice2" /> Choice #2
</form>
By using this, only 1 option can be checked at one time
Use Radio, and they must have the same name="".