Quad-state checkbox - html

I can have a tri-state checkbox with indeterminate, but I need to rotate 4 states. So how can I make something to look like a quad state checkbox? Is my only option to create a set of images to look like a checkbox with various icons or is there a more elegant way?

You can achieve similar effect (cycled state something) with radio buttons and a bit of CSS (CSS3), without JavaScript:
.cyclestate {
display: inline-grid;
}
.cyclestate label {
grid-area: 1 / 1;
background-color: white;
color: black;
z-index: 0;
opacity: 0;
}
.cyclestate input {
position: absolute;
z-index: -1;
}
.cyclestate input:checked + label {
opacity: 1;
}
.cyclestate input:first-of-type + label {
z-index: 2;
}
.cyclestate input:checked + label + input + label {
opacity: 0;
z-index: 2;
}
/* -- Unrelated and accessibility -- */
.cyclestate { border: none; padding: 0; }
.cyclestate legend { position: absolute; }
.cyclestate label { padding: 1em; text-align: center; display: inline-block; cursor: pointer; user-select: none;
}
.cyclestate label::before { content: '← '; }
.cyclestate label::after { content: ' →'; }
.cyclestate label::before, label::after { color: transparent; }
.cyclestate input:focus-visible + label { outline: solid; }
.cyclestate input:focus-visible + label::before,
.cyclestate input:focus-visible + label::after { color: currentcolor; }
:root { background: dimgray; color: snow; }
:link { color: aqua; } :visited { color: lime; }
<fieldset class="cyclestate" id="x">
<legend>Pick a number</legend>
<input id="one" type="radio" name="r" checked>
<label for="one">One</label>
<input id="two" type="radio" name="r">
<label for="two">Two</label>
<input id="three" type="radio" name="r">
<label for="three">Two Plus One</label>
<input id="four" type="radio" name="r">
<label for="four">Four</label>
</fieldset>
<p><button onclick="x.classList.toggle('cyclestate')">Toggle condenseness ↑</button>
Basically this approach overlays labels over each other (leveraging quite modern CSS techniques (grid)) and makes label of the NEXT unchecked input transparent and raised above all others. In effect what is visible is checked input's label, but click / tap events are captured by that transparent sibling overlay. This preserves semantics and accessibility.
(Caution: haven't thoroughly tested with screen readers nor older browsers.)
Original 2015 snippet with really dubious accessibility, use with caution (or better do not use at all):
.cyclestate input:not(b),
.cyclestate input:not(b) + label {
display: none;
}
.cyclestate input:checked + label {
display: inline-block;
width: 4em;
text-align: center;
-webkit-user-select: none;
-moz-user-select: none;
-webkit-appearance: button;
-moz-appearance: button;
}
<span class="cyclestate">
<input type="radio" name="foo" value="one" id="one" checked>
<label for="two">one</label>
<input type="radio" name="foo" value="two" id="two">
<label for="three">two</label>
<input type="radio" name="foo" value="three" id="three">
<label for="four">three</label>
<input type="radio" name="foo" value="four" id="four">
<label for="one">four</label>
</span>
With this you'll have space-conserving visual representation of multiple state at one place and readable value on the server side.
Be warned that such usage of label is a kinda exploit: label targets different input than it describes.

A checkbox can be made to look like it can have three states, but it will truly only ever have two.
Also, the "indeterminate" visual state is not reachable from the default user interface and has to be triggered programatically:
var box = document.getElementById('box'),
checked = document.getElementById('checked'),
indet = document.getElementById('indet');
var update = function()
{
checked.textContent = box.checked;
indet.textContent = box.indeterminate;
};
box.addEventListener('click', update);
document.getElementById('a').addEventListener('click', function()
{
box.checked = true;
update();
});
document.getElementById('b').addEventListener('click', function()
{
box.checked = false;
update();
});
document.getElementById('c').addEventListener('click', function()
{
box.indeterminate = true;
update();
});
document.getElementById('d').addEventListener('click', function()
{
box.indeterminate = false;
update();
});
<input type="checkbox" id="box"><br>
<button id="a">On</button> <button id="b">Off</button><br>
<button id="c">Indeterminate</button> <button id="d">Clear indeterminate</button>
<div>box.checked: <span id="checked"></span></div>
<div>box.indeterminate: <span id="indet"></span></div>
So to answer your question:
There is no native way to add a fourth visual (yet even actual) state to a checkbox, but this does not mean that you have to resort to images.
It's easy enough to style an element to look like a checkbox and use some JS to simulate a state rotation:
var state = 0;
var toggle = document.getElementById('toggle');
toggle.addEventListener('click', function()
{
state = (state + 1) % 4;
toggle.className = 'state' + state;
});
#toggle
{
border: solid 1px #666;
border-radius: 3px;
width: 14px;
height: 14px;
}
.state0
{
background: linear-gradient(#DDD, #FFF);
}
.state1
{
background: linear-gradient(#F00, #C00);
}
.state2
{
background: linear-gradient(#FF0, #CC0);
}
.state3
{
background: linear-gradient(#0F0, #0A0);
}
<div id="toggle" class="state0"></div>
But in the end you will have to use something other than actual checkboxes.
Apart from that, having a checkbox-like thing with more that two states is really counter-intuitive, and I kindly ask you to go with something that was made for multiple states, like radio buttons or a dropdown list.

It's not possible today. For others reading, a good reference about the using the three states including indeterminate: https://css-tricks.com/indeterminate-checkboxes/
You'll need to create a different solution using your own icons and in a sense create a custom form control. A possible design that fulfills your need to have multiple states:
Use a list of icons, like the star system in Gmail. When the star is clicked in Gmail it changes color and eventually after enough clicks (up to 10 I think) other icons like exclamation point and checkmark begin displaying.
Associate your different state requirements with the currently used icon (or index of the underlying icon list).

Related

Is there a way to clear an input type="number" like when using type="search"

I have a Bootstrap 4 form with various inputs on some number, some text and others email.
I have already sorted my text inputs by adding the below, which is displaying the 'x' in the input
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: searchfield-cancel-button;
}
But I want the same for my 'number' and 'email' inputs
I tried using the below to see if it works, but it doesn't
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: searchfield-cancel-button;
}
I have also used the below CSS to remove the arrows when using type='number' and again it works fine
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
Thought I'd ask before I revert to using regex for my number inputs
HTML
<input id="one" class="form-control" type="search" value="Test company name" />
<input id="two" class="form-control" type="number" value="123456" />
<input id="two" class="form-control" type="number" value="12345634" />
As you can see the arrows are not displaying for my number inputs which is what I want.
There is good and bad news. The bad news first: the webkit cancel button is only available to input fields of type search.
The good news: you can create the button yourself.
Copy and paste the following CSS:
.close-button {
color: #1e52e3;
font-size: 12pt;
font-family: monospace,sans-serif;
font-weight: 600;
position: absolute;
z-index: 2;
display: none;
}
.close-button:hover {
cursor: pointer;
}
and change the font-size to what is suitable by trial and error.
Then add this Javascript before the </body> tag in the relevant HTML:
<script>
var fields = document.querySelectorAll('input[type=number],input[type=email]');
fields.forEach(function(input) {
let x = document.createElement('span');
x.classList.add('close-button');
x.innerHTML = 'x';
x.style.left = input.clientWidth - 15;
x.onmousedown = removeContent;
input.parentNode.insertBefore(x, input);
input.oninput = toggleCloseButton;
input.addEventListener( 'focusin', toggleCloseButton);
input.addEventListener( 'focusout', hideCloseButton);
});
function toggleCloseButton() {
if (this.value.length > 0) {
this.previousSibling.style.display = 'block';
} else {
this.previousSibling.style.display = 'none';
}
}
function hideCloseButton() {
this.previousSibling.style.display = 'none';
}
function removeContent(){
this.nextSibling.value = '';
this.nextSibling.focus();
}
</script>

How to style the parent label of a checked radio input

I need to stylize some radio inputs. I tried some solutions from here but none worked for me. Can someone please take a look at this code and tell me what can I do?
This is the HTML:
<div class="controls">
<table>
<tbody>
<tr>
<td>
<label class="radio">
<input type="radio" name="ad_caroserie" value="0">Berlina
</label>
</td>
<td>
<label class="radio">
<input type="radio" name="ad_caroserie" value="1">Break
</label>
</td>
<td>
<label class="radio">
<input type="radio" name="ad_caroserie" value="2">Cabrio
</label>
</td>
</tr>
</tbody>
</table>
</div>
And the CSS:
label.radio {
background: #fcb608;
}
.radio input {
display: none;
}
label.radio input[type="radio"]:checked + label {
background: #000 !important;
border: 1px solid green;
padding: 2px 10px;
}
The CSS doesn't have the desired effect; Can you please help me?
This is some related excerpts of JS:
//If checkboxes or radio buttons, special treatment
else if (jQ('input[name="'+parentname+'"]').is(':radio') || jQ('input[name="'+parentname+'[]"]').is(':checkbox')) {
var find = false;
var allVals = [];
jQ("input:checked").each(function() {
for(var i = 0; i < parentvalues.length; i++) {
if (jQ(this).val() == parentvalues[i] && find == false) {
jQ('#adminForm #f'+child).show();
jQ('#adminForm #row_'+child).show();
find = true;
}
}
});
if (find == false) {
jQ('#adminForm #f'+child).hide();
jQ('#adminForm #row_'+child).hide();
//cleanup child field
if (jQ('#adminForm #f'+child).is(':checkbox') || jQ('#adminForm #f'+child).is(':radio')) {
jQ('#adminForm #f'+child).attr('checked', false);
}
else {
if (cleanValue == true) {
jQ('#adminForm #f'+child).val('');
}
}
}
}
else {
var find = false;
for(var i = 0; i < parentvalues.length; i++) {
if (jQ('#adminForm #f'+parentname).val() == parentvalues[i] && find == false) {
jQ('#adminForm #f'+child).show();
jQ('#adminForm #row_'+child).show();
find = true;
}
}
if(find == false) {
jQ('#adminForm #f'+child).hide();
jQ('#adminForm #row_'+child).hide();
//cleanup child field
if (jQ('#adminForm #f'+child).is(':checkbox') || jQ('#adminForm #f'+child).is(':radio')) {
jQ('#adminForm #f'+child).attr('checked', false);
}
else {
if (cleanValue == true) {
jQ('#adminForm #f'+child).val('');
}
}
}
}
}
function dependency(child,parentname,parentvalue) {
var parentvalues = parentvalue.split(",");
//if checkboxes
jQ('input[name="'+parentname+'[]"]').change(function() {
checkdependency(child,parentname,parentvalues,true);
//if checkboxes
jQ('input[name="'+child+'[]"]').change();
jQ('input[name="'+child+'"]').change();
jQ('#'+child).change();
});
//if buttons radio
jQ('input[name="'+parentname+'"]').change(function() {
checkdependency(child,parentname,parentvalues,true);
//if checkboxes
jQ('input[name="'+child+'[]"]').change();
jQ('input[name="'+child+'"]').change();
jQ('#'+child).change();
});
jQ('#f'+parentname).click(function() {
checkdependency(child,parentname,parentvalues,true);
//if checkboxes
jQ('input[name="'+child+'[]"]').change();
jQ('input[name="'+child+'"]').change();
jQ('#f'+child).change();
});
checkdependency(child,parentname,parentvalues,false);
}
A possibility
At my time of posting, I am not exactly sure what the desired layout should be, but there is one specific problem in the attempted CSS that needs to be addressed.
The adjacent siblings selector:
... separates two selectors and matches the second element only if it immediately follows the first element.
If the <input> is a child of the <label>, it isn't adjacent, so while:
label.radio input[type="radio"]:checked + label
is looking for a label immediately following a :checked input inside a label with the class .radio, nothing like that exists.
To alter the styling of the label in this case, would require a selector that affected the parent, which currently isn't possible.
So, to select the label of the :checked input, we need the label to be adjacent, not the parent.
We can use the for="id" attribute:
A <label> can be associated with a control either by placing the control element inside the <label> element, or by using the for attribute.
As I said, I'm not exactly sure what the desired layout should be, but here's an example using the for attribute, that doesn't look too bad.
div {
display: inline-block;
position: relative;
}
label {
background: #fcb608;
padding: 2px 10px 2px 1.5em;
border: 1px solid transparent; /* keeps layout from jumping */
}
input {
position: absolute;
}
input[type="radio"]:checked + label {
background: #000;
border-color: green;
color: white;
}
<div>
<input id="id1" type="radio" name="ad_caroserie" value="0">
<label for="id1" class="radio">Berlina</label>
</div>
<div>
<input id="id2" type="radio" name="ad_caroserie" value="1">
<label for="id2" class="radio">Break</label>
</div>
<div>
<input id="id3" type="radio" name="ad_caroserie" value="2">
<label for="id3" class="radio">Cabrio</label>
</div>
With <input> as a child of <label>
Using a small JavaScript handler listening for changes to the <form>.
If a change is detected, the triggered function checks if an <input type="radio"> was changed, and if so, if it has a <label> as its parentElement.
If that's true, it checks to see if there's an identically named <input type="radio"> that's a child of a <label> element with the class .checked.
If there is, it removes the class from the <label> before applying the same class to the <label> parent of the <input> target that triggered the whole thing.
let form = document.querySelector( "form" );
form.addEventListener( "change", ( evt ) => {
let trg = evt.target,
trg_par = trg.parentElement;
if ( trg.type === "radio" && trg_par &&
trg_par.tagName.toLowerCase() === "label" ) {
let prior = form.querySelector( 'label.checked input[name="' +
trg.name + '"]' );
if ( prior ) {
prior.parentElement.classList.remove( "checked" );
}
trg_par.classList.add( "checked" );
}
}, false );
label {
background: #fcb608;
padding: 2px 10px 2px 0;
border: 1px solid transparent; /* keeps layout from jumping */
}
label.checked {
background: #000;
border-color: green;
color: white;
}
<form>
<label class="radio"><input type="radio" name="ad_caroserie" value="0">Berlina</label>
<label class="radio"><input type="radio" name="ad_caroserie" value="1">Break</label>
<label class="radio"><input type="radio" name="ad_caroserie" value="2">Cabrio</label>
</form>
Without JavaScript things get difficult (per my original explanation of why it's best to use the for attribute in this case).
We can use the appearance property (with prefixes and reasonable support) to effectively hide the user-agent radio GUI, then use the remaining faceless element to build a fake background for the <label>.
This is very hacky and a great deal less dynamic than the default, since some absolute positioning and specific dimensions are required to pull it off.
It kind of works (in most browsers), but is tricky to enforce sitewide.
Something to play around with though :-)
input {
position: absolute;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
width: 5em;
height: 1.5em;
z-index: -1;
background: #fcb608;
border: 1px solid transparent;
margin: -.1em -.8em;
outline: 0;
}
label {
display: inline-block;
width: 5em;
color: white;
text-shadow: 1px 1px 0px black;
}
input[type="radio"]:checked {
background: #000;
border-color: green;
}
<label class="radio"><input type="radio" name="ad_caroserie" value="0">Berlina</label>
<label class="radio"><input type="radio" name="ad_caroserie" value="1">Break</label>
<label class="radio"><input type="radio" name="ad_caroserie" value="2">Cabrio</label>
Just use jQuery with a new css class "selected" something like this:
on start:
$("input[name='ad_caroserie']:checked").parent().addClass("selected");
and onchange:
$('input[type=radio][name=ad_caroserie]').change(function() {
$("input[name='ad_caroserie']").parent().removeClass("selected");
$("input[name='ad_caroserie']:checked").parent().addClass("selected");
// console.log($("input[name='ad_caroserie']:checked").val());
});
After trying so many time with pure HTML and CSS. I am settling with this simple solution with JavaScript. I think this will help.
function check(btn) {
let label = btn.children;
label[0].checked = true;
}
.lib-radio {
color: #1e1e1e;
font-size: 1.0em;
border-radius: 31px;
font-weight: 400;
width: 450px;
height: 40px;
border: 2px solid black;
padding: 0.5em;
font-family: Lato;
margin: 10px 0 0 0;
}
.lib-radio:hover {
background-color: #000;
color: #FFF;
}
<div class="lib-one-input">
<p class="lib-form-label">Select your gender</p>
<div class="lib-radio" onclick="check(this)">
<input id="s1yes" type="radio" name="mcq1" value="male">
<label for="s1yes"> Yes, our researchers authored it</label><br />
</div>
<div class="lib-radio" onclick="check(this)">
<input id="s1no" type="radio" name="mcq1" value="female">
<label for="s1no"> No, we didn&#39t author it </label><br />
</div>
</div>
<form id="button-form">
<fieldset id="button-set">
<label for="button-1">
<input type="radio" id="button-1" name="button-group"/>
</label>
<label for="button-2">
<input type="radio" id="button-2" name="button-group"/>
</label>
<label for="button-3">
<input type="radio" id="button-3" name="button-group"/>
</label>
</fieldset>
</form>
<style>
#button-set label > * {
opacity: 0; <!-- hideing the radio buttons -->
}
</style>
<script>
function setColor(color) {
document.getElementsByName("button-group").forEach(node => {
node.checked === true
? (node.parentElement.style.background = color)
: (node.parentElement.style.background = "rgba(0, 0, 0, 0.5)");
});
}
window.onload = () => {
document.getElementById("button-form").onchange = evt => {
switch (evt.target) {
case document.getElementsById("button-1"):
setColor("rgba(0, 150, 0, 0.5)");
break;
case document.getElementsById("button-2"):
setColor("rgba(250, 200, 0, 0.5)");
break;
case document.getElementsById("button-3"):
setColor("rgba(250, 0, 0, 0.5)");
break;
}
};
};
</script>

HTML.Checkboxfor to Toggle button HTML?

Is there anyway to convert the #HTML.Checkboxfor into toggle html button?
I have refer the toggle button style from https://www.w3schools.com/howto/howto_css_switch.asp
Currently its working by using :
#Html.CheckBoxFor(model => model.IsEnabled, new { #checked = "checked", #Name = "DelegateChkBox" })
Changed to:
<td class="slider-td">
<label class="switch">
<input name="DelegateChkBox" id="IsEnabled" type="checkbox" value="true" data-val-required="The IsEnabled field is required." data-val="true" data-bind="checked: IsEnabled">
<div class="slider round"></div>
</label>
</td>
The toggle button only works and postback to controller if it's CHECKED:
else it won't postback to controller by default UNCHECKED :
Any help will be appreciated!
Thanks!
Based from some experiments and findings, I found that you need to include hidden field generated by the helper (Html.CheckBoxFor) as shown by styling below:
<style type="text/css">
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
}
/* include generated hidden field here */
input[type="checkbox"]:checked + input[type="hidden"] + .slider,
input[type="checkbox"]:checked + .slider {
background-color: #2196F3;
}
/* include generated hidden field here */
input[type="checkbox"]:focus + input[type="hidden"] + .slider,
input[type="checkbox"]:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
/* include generated hidden field here */
input[type="checkbox"]:checked + input[type="hidden"] + .slider:before,
input[type="checkbox"]:checked + .slider:before {
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
View usage example (just change <input type="checkbox" /> to #Html.CheckBoxFor):
<label class="switch">
#Html.CheckBoxFor(model => model.IsEnabled)
<div class="slider round">
</div>
</label>
To ensure the checkbox is being checked in client-side before doing postback, you can use jQuery code below:
// change event may modified to other events
$("#IsEnabled").change(function () {
// check if checkbox is being checked
// taken from /a/23007488 by Alexandru Chichinete
if ($("#IsEnabled").is(":checked"))
{
// enable form submit
}
});
Demo example: .NET Fiddle
References:
Styling checkboxes in MVC 4
How can I apply a CSS style to Html.CheckBoxFor in MVC 5
#Html.checkboxfor - Get checkbox value using id
This is very simple, I have implemented in my project.
You just need to keep the value of checkbox in a HiddenFor.
#Html.HiddenFor(model => model.IsEnabled, new{#id = "DelegateChkBox"})
Then display your Toggle button in the page. For example,
<td class="slider-td">
<label class="switch">
<input name="DelegateChkBox" id="IsEnabled" type="checkbox" value="true"
data-val-required="The IsEnabled field is required." data-val="true"
data-bind="checked: IsEnabled">
<div class="slider round"></div>
</label>
</td>
So now when user check/uncheck the checkbox, call a jquery method and assign the value to #Html.HiddenFor(..). That's it.
$('#IsEnabled').change(function(){
if($(this).val()==true)
{
$('#DelegateChkBox').val(true);
}
else
{
$('#DelegateChkBox').val(false);
}
})
Please check once this if ($("this").is(":checked")) instead if($(this).val()==true) and assign the value accordingly. Little busy, could not check my code :)
That's it. Now when you post back your page, you already set value to your IsEnabled field.
In Get method you have to do like the same. You just need to check the value of your HiddenFor() and take that value and assign to your HTML Toggle button in pageload event of js.
$(function() {
if($('#DelegateChkBox').val()== true)
{
$('#IsEnabled').prop("disabled", false);
}
else
{
$('#IsEnabled').prop("disabled", true);
}
});
That's it. Please change the HTML accordingly.

CSS animated slide down when lower element gets removed (elements in container with relative position)

I've got a script which create a simple container for error message boxes and if needed create these boxes in it. The container got a fixed width, like the boxes but not a fixed height, so the boxes stack about each other in a vertical row. Also the boxes in the container got position: relative.
Picture of the container & boxes:
Now what I want is, that if you remove a box, the above boxes slides down, animated. I tried it with CSS transitions but it didn't work (I'm pretty new to transitions, so I don't even know if it's possible with position: relative because the top and bottom properties aren't set).
Example [See picture for reference]: I click on box 3, box 3 gets removed, box 1 and 2 slide down next to 4, 5, and 6. This works, but it isn't animated yet.
Question: Is it possible to do this with CSS, and if so, how?
HTML:
<div class="popBoxContainer popBoxContainerRight">
<div class="popBox error" onclick="this.parentNode.removeChild(this);" title="click to close">
<p class="popBox__title">error</p>
<span class="popBox__msg">Test-Box-1</span>
</div>
<div class="popBox error" onclick="this.parentNode.removeChild(this);" title="click to close">
<p class="popBox__title">error</p>
<span class="popBox__msg">Test-Box-2</span>
</div>
<div class="popBox error" onclick="this.parentNode.removeChild(this);" title="click to close">
<p class="popBox__title">error</p>
<span class="popBox__msg">Test-Box-3</span>
</div>
</div>
CSS:
.popBoxContainer {
position: fixed;
width: 300px;
bottom: 0;
padding: 0 10px 15px 10px;
}
.popBoxContainerRight {
right: 0;
}
.popBoxContainerLeft {
left: 0;
}
.popBox {
padding: 15px 25px;
margin-top: 20px;
background-color: #fff;
box-shadow: 2px 2px 3px rgba(0,0,0,.03);
opacity: 0.8;
}
.popBox:hover {
opacity: 1;
cursor: pointer;
}
.popBox .popBox__title {
font-family: 'Oswald', sans-serif;
font-size: 18px;
margin-bottom: 8px;
text-transform: uppercase;
}
.popBox.error .popBox__title {
color: #FF5252;
}
.popBox.warning .popBox__title {
color: #FFDB70;
}
.popBox .popBox__msg {
color: #4c4c4c;
}
JS:
function initPopBox(side) {
if(document.getElementsByClassName('popBoxContainer').length < 1) {
var box = document.createElement('div');
box.className = 'popBoxContainer';
if(side != "") {
if(side == "left") {
box.className += ' popBoxContainerLeft';
} else {
box.className += ' popBoxContainerRight';
}
} else {
box.className += ' popBoxContainerRight';
}
document.body.appendChild(box);
}
}
function popBox(type, txt) {
var boxCon = document.getElementsByClassName('popBoxContainer');
if(boxCon.length < 1) {
initPopBox();
}
switch(type) {
case 'error':
boxCon[0].innerHTML += '<div class="popBox error" onclick="this.parentNode.removeChild(this);" title="click to close"><p class="popBox__title">error</p><span class="popBox__msg">'+txt+'</span></div>';
break;
case 'warning':
boxCon[0].innerHTML += '<div class="popBox warning" onclick="this.parentNode.removeChild(this);" title="click to close"><p class="popBox__title">warning</p><span class="popBox__msg">'+txt+'</span></div>';
break;
default:
boxCon[0].innerHTML += '<div class="popBox error" onclick="this.parentNode.removeChild(this);" title="click to close"><p class="popBox__title">error</p><span class="popBox__msg">'+txt+'</span></div>';
break;
}
}
Without knowing your HTML it's hard to really solve. That being said, you can't get the desired behavior using only CSS, like Paulie_D said in the comments above. You can get close (if you don't need a sliding animation).
Here is a combination of CSS and jQuery that does what you want:
$("label").click(function() {
$(this).delay(600).queue(function (next) {
$(this).css('display', 'none');
next();
});
});
body {
margin: 0;
}
input[type="checkbox"] {
position: absolute;
left: -9999px;
}
.container {
width: 235px;
margin: 0 auto;
background: lightgrey;
padding: 1px 0;
}
label {
display: block;
background: white;
margin: 16px;
width: 200px;
height: 50px;
color: #c95959;
position: relative;
}
p {
margin-top: 12px;
color: #3e3b3b;
}
#box1:checked ~ .container .label1, #box2:checked ~ .container .label2, #box3:checked ~ .container .label3, #box4:checked ~ .container .label4, #box5:checked ~ .container .label5, #box6:checked ~ .container .label6 {
opacity: 0;
transition: opacity 0.7s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="checkbox" id="box1"/>
<input type="checkbox" id="box2"/>
<input type="checkbox" id="box3"/>
<input type="checkbox" id="box4"/>
<input type="checkbox" id="box5"/>
<input type="checkbox" id="box6"/>
<div class="container">
<label class="label1" for="box1">
<span>ERROR</span>
<p>Test-Error-Box-1</p>
</label>
<label class="label2" for="box2">
<span>ERROR</span>
<p>Test-Error-Box-2</p>
</label>
<label class="label3" for="box3">
<span>ERROR</span>
<p>Test-Error-Box-3</p>
</label>
<label class="label4" for="box4">
<span>ERROR</span>
<p>Test-Error-Box-4</p>
</label>
<label class="label5" for="box5">
<span>ERROR</span>
<p>Test-Error-Box-5</p>
</label>
<label class="label6" for="box6">
<span>ERROR</span>
<p>Test-Error-Box-6</p>
</label>
</div>
JSFIDDLE Example
You need to use max-height here. On the original element have a max-height of whatever height you want it to be. Using height auto I don't think will work with this. There's a good example at: http://codepen.io/LFeh/pen/ICkwe
Take a look at this code:
.element{
-webskit-transition: max-height 1s;
max-height:0px;
}
.parentelement:hover childelement {
max-height:50px;
}

make text act like a checkbox

I'm setting up a form, is it possible to make a textstring act like a checkbox and for example highlight it when clicked, without actually showing the checkbox?
edit:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script>
<script type="text/javascript">
$(function(){
$('.fake-check').on('click',function(){
if($(this).hasClass('checked')){
$(this).removeClass('checked').children('input').val('');
}else{
$(this).addClass('checked').children('input').val($(this).data('val'));
}
});
});
</script>
<link href="test.css" rel="stylesheet" type="text/css" />
<a class="fake-check" data-val="somevalue">Something<input type="text" name="somefield"></a>
Yes, given the following HTML:
​<input type="checkbox" id="test" /><label for="test">Click to check</label>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
And the CSS:
​label {
color: #f00;
}
input {
display: none;
}
input[type=checkbox]:checked + label {
color: #0f0;
}
​
label {
color: #f00;
}
input {
display: none;
}
input[type=checkbox]:checked + label {
color: #0f0;
}
<input type="checkbox" id="test" checked/>
<label for="test">Click to check</label>
JS Fiddle demo.
This is, of course, dependent on browsers implementing the :checked pseudo-class.
I've used the attribute-equals notation to specify the label elements that follow a checked input of type="checkbox", but as only a checkbox or radio can be selected, that specificity might be safely omitted.
You can also, in compliant browsers, use the same approach in concert with the ::after pseudo-element and its content property, change the text to reflect the state of the checkbox. For example, with the following HTML:
​<input type="checkbox" id="test" /><label for="test">Click to</label>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
And the CSS:
​label {
color: #f00;
}
input {
display: none;
}
input[type=checkbox]:checked + label {
color: #0f0;
}
input[type=checkbox] + label::after {
content: ' check';
}
input[type=checkbox]:checked + label::after {
content: ' uncheck';
}
​
label {
color: #f00;
}
input {
display: none;
}
input[type=checkbox]:checked + label {
color: #0f0;
}
input[type=checkbox] + label::after {
content: ' check';
}
input[type=checkbox]:checked + label::after {
content: ' uncheck';
}
<input type="checkbox" id="test" checked/>
<label for="test">Click to</label>
References:
Adjacent-sibling (+) combinator.
::before`::after` pseudo-elements.
:checked pseudo-class.
This will work, but only on CSS3 browsers:
<input type="checkbox" id="a" name="a" class="hideMe"> ​<label for="a">Something</label>
​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
.hideMe {
height:0;
width:0;
visibility:hidden;
}
input:checked + label {
background-color:#e0e0e0
}
You could use jQuery and do this pretty easily:
HTML:
<a class="fake-check" data-val="somevalue">
Something
<input type="hidden" name="somefield">
</a>
CSS:
.checked { background: red; }
JavaScript:
$('.fake-check').on('click',function(){
if($(this).hasClass('checked')){
$(this).removeClass('checked').children('input').val('');
}else{
$(this).addClass('checked').children('input').val($(this).data('val'));
}
});
Here's the fiddle: http://jsfiddle.net/vZVNC/
Try this code
<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
var is_highlighted = false;
function textclicked(text){
if(is_highlighted == false){
text.style.background = '#FFFF99';
is_highlighted = true;
}
else{
text.style.background = 'none';
is_highlighted = false;
}
}
</script>
</head>
<body>
<a style="cursor:pointer;" onclick="javascript:textclicked(this)">some text which act like checkbox</a>
</body>
</html>