How to toggle a checkbox when a link is clicked? - html

I'm trying to do a pretty simple checkbox hack in an HTML email to make some basic in-email interactivity.
Something like the following:
<style>
input:checked + div {
text-decoration: line-through;
}
</style>
<label>
<input type="checkbox" style="display:none"/>
<div>A todo item</div>
</label>
Whenever the todo item is clicked, I can apply some styling marking it
done.
But if I make the todo item a link:
<style>
input:checked + a {
text-decoration: line-through;
}
</style>
<label>
<input type="checkbox" style="display:none"/>
Open Google
</label>
The checkbox isn't toggled when the link is clicked.
Here's a codepen to demonstrate.
Is there any way to get the link to open, and the checkbox to toggle? As this is destined for an HTML email, any javascript solution is off the table.

The answer is: you cannot without JS.
That HTML setup makes nested interactive contents. The fact is that the <a> tag receives the click event and that cancels the click on the label. You need some JS! This way the natural behaviour of the checkbox is not altered, i.e. you can un-click:
<style>
input:checked+a {
text-decoration: line-through;
}
</style>
<label for="myInput">
<input id="myInput" type="checkbox" style="display:none"/>
Open Google
</label>
Working Demo

EDIT
As it is for email and you cant use JS, just add a tabindex to a tag and a css. Its the closest you can get without using javascript
Working Demo below:
label {
display: block;
padding: 10px 0;
}
input:checked + div{
text-decoration: line-through;
}
a:focus{
text-decoration: line-through;outline:0;}
<label>
<input type="checkbox" style="display:none"/>
<div>Todo Item</div>
</label>
<label>
<input type="checkbox" style="display:none"/>
<div>Another todo Item</div>
</label>
<label>
<input type="checkbox" style="display:none" id='btnControl'/>
Open Google
</label>

JS
function myFunction() {
document.getElementById("me").style.textDecoration = "line-through";
}
HTML
<label>
<input type="checkbox" style="display:none"/>
<a href="http://www.google.com" id="me" onclick="myFunction()" target="_blank">Open
Google</a>
</label>

Related

Using only HTML and CSS, how can I used the "checked" status of a radio button to show/hide conditional form fields?

I have this working for a checkbox. When I select the checkbox, my two radio buttons appear. But now, if I select one of the radio buttons, I want a block of address fields to appear. I'm not sure why the same thing that is working for the checkbox isn't working for the radio button.
<form>
<input class="no22XAddressRadioButtons_activator" type="checkbox">
Check if you do not have a physical address within the Oakland Beat 22X boundary.
<div class="no22XAddressRadioButtons">
<input class="outsideAddress_activator" type="radio" name="no22XAddress" id="outsideAddress_activator">
<label for="outsideAddress_activator">My physical address is outside of the 22X boundary.</label><br>
<input class="noAddress" type="radio" name="no22XAddress" id="no22XAddress">
<label for="no22XAddress">I don't have a physical address.</label><br>
</div>
<!--Begin conditional address-->
<div class="outsideAddressTextFields">
Enter your physical address including city, state, and zip code.
<input class="outsideAddress" type="text">
<label class="labelText" for="outsideAddress">Number and Street</label>
<input type="text" name="outsideAddress" id="outsideAddress" required>
</div>
</form>
/* Begin conditional radio buttons */
.no22XAddressRadioButtons {
display: none;
}
.no22XAddressRadioButtons_activator {
margin-left: 30px;
}
.no22XAddressRadioButtons_activator:checked + .no22XAddressRadioButtons {
display: block;
margin-left: 45px;
}
/* Begin conditional address */
.outsideAddressTextFields{
display: none;
}
.outsideAddress_activator:checked + .outsideAddressTextFields {
display: block;
}
Thats because you're using +, an Adjacent sibling combinator. Which means the block that you need to be made visible, while an input is checked has to be right after the said input. You can use a General sibling combinator which I think might be helpful in you case. I've attached a modified code snippet to demonstrate.
As a side note: use labels to associate an input to a caption.
You can click the associated label to focus/activate the input, as well as the input itself. This increased hit area provides an advantage to anyone trying to activate the input, including those using a touch-screen device.
/* Begin conditional radio buttons */
.no22XAddressRadioButtons {
display: none;
}
.no22XAddressRadioButtons_activator {
margin-left: 30px;
}
.no22XAddressRadioButtons_activator:checked ~ .no22XAddressRadioButtons {
display: block;
margin-left: 45px;
}
/* Begin conditional address */
.outsideAddressTextFields{
display: none;
}
.outsideAddress_activator:checked ~ .outsideAddressTextFields {
display: block;
}
<form>
<input id="addressCheck" class="no22XAddressRadioButtons_activator" type="checkbox">
<label for="addressCheck">Check if you do not have a physical address within the Oakland Beat 22X boundary.</label>
<div class="no22XAddressRadioButtons">
<div>
<input class="outsideAddress_activator" type="radio" name="no22XAddress" id="outsideAddress_activator">
<label for="outsideAddress_activator">My physical address is outside of the 22X boundary.</label>
<!--Begin conditional address-->
<div class="outsideAddressTextFields">
<div>
<label for="insideAddress">Enter your physical address including city, state, and zip code.</label>
<input class="outsideAddress" type="text" id="insideAddress">
</div>
<div>
<label class="labelText" for="outsideAddress">Number and Street</label>
<input type="text" name="outsideAddress" id="outsideAddress" required>
</div>
</div>
</div>
<div>
<input class="noAddress" type="radio" name="no22XAddress" id="no22XAddress">
<label for="no22XAddress">I don't have a physical address.</label><br>
</div>
</div>
</form>

div option value to form value

We have built an html form which contains basic stuff with input and option fields. This works good!
Then we have some fields to choose an option:
<h2 class="space">Franchise</h2>
<div class="spa2">
<p option value="300" data-value="300" class="s2">300</p>
<p option value="500" data-value="500" class="s2 selected">500</p>
<p option value="1000" data-value="1000" class="s2">1000 </p>
<p option value="1500" data-value="1500" class="s2">1500</p>
<p option value="2000" data-value="2000" class="s2">2000 </p>
<p option value="2500" data-value="2500" class="s2">2500</p>
</div>
This displays buttons are to choose specific values, but respective data is not transferred with POST because there are some non-html form type fields.
Is there a way to cheat and tell that this are form fields without change anything?
The code displays this buttons here:
Change them into <form> elements
You're asking the wrong question. This look can be achieved with form elements, which is easier and more reliable than "cheating".
HTML
<form>
<input id="one" type="radio" name="choices" />
<label for="one">300</label>
<input id="two" type="radio" name="choices" />
<label for="two">500</label>
<input id="three" type="radio" name="choices" />
<label for="three">1000</label>
</form>
This example consists of radio inputs. Use this if there is only one choice, otherwise make the type checkbox if you can have multiple choices.
The label should follow the input so that the corresponding label can be styled when the input is checked.
The label is connected to its corresponding radio input with the matching for and id attributes.
The radio input options are linked with the same name="choices" and only one option can be selected.
CSS
input[type=radio] {
display: none;
}
input[type=radio] + label {
padding: 10px;
border: solid 1px;
cursor: pointer;
}
input[type=radio]:checked + label {
background: red;
}
Hide the radio buttons with display: none
Style the labels that follow the radio buttons input[type=radio] + label
Change the styles when a radio button is selected with input[type=radio]:checked + label
Make the cursor a pointer with cursor: pointer on the labels
Full Example
form {
margin: 20px;
}
input[type=radio] {
display: none;
}
input[type=radio]+label {
padding: 10px;
border: solid 1px;
cursor: pointer;
}
input[type=radio]:checked+label {
background: red;
}
<form>
<input id="one" type="radio" name="choices" />
<label for="one">300</label>
<input id="two" type="radio" name="choices" />
<label for="two">500</label>
<input id="three" type="radio" name="choices" />
<label for="three">1000</label>
</form>

Change background-image on radio button checked

I am currently creating a form in Wordpress using gravity forms and an image plugin, that allows me to insert images instead of radio buttons. However, I would like the image to change when the radio button is checked. My issue is that I have no idea how to approach this, and how can I target the background-image when it is directly styled in the HTML?
<div class='ginput_container ginput_container_radio'>
<ul class='gfield_radio' id='input_1_2'>
<li class='gchoice_1_2_0'><input name='input_2' type='radio' value='' id='choice_1_2_0' tabindex='1' /><label for='choice_1_2_0' id='label_1_2_0'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image1.com)"><img src="http://image1.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
<li class='gchoice_1_2_1'><input name='input_2' type='radio' value='' id='choice_1_2_1' tabindex='2' /><label for='choice_1_2_1' id='label_1_2_1'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image2.com)"><img src="http://image2.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
<li class='gchoice_1_2_2'><input name='input_2' type='radio' value='' id='choice_1_2_2' tabindex='3' /><label for='choice_1_2_2' id='label_1_2_2'><span class="image-choices-choice-image-wrap" style="background-image:url(http://image3.com)"><img src="http://image3.com" alt="" class="image-choices-choice-image" /></span><span class="image-choices-choice-text"></span></label></li>
</ul>
</div>
I would highly appreciate any help I can get to solve this issue.
Let me know if you need further information, thanks
You could be more specific on which image you want to change and from which radio button do you want to trigger the event. But still I'll give it a shot.
<input name='input_2' type='radio' value='' id='choice_1_2_0' tabindex='1' />
<div class="image-choices-choice-image-wrap" style="background-image:url(https://cdn.kimkim.com/files/a/content_articles/featured_photos/d1ea2d6f6d1aa470f661fa661bd0e2fb14fd2d2c/medium-886981758498052b0532dc3a96b98adf.jpg);height:200px;width:400px">
<script>
$('#choice_1_2_0').click(function(){
// If radiobuton is checked
if ($(this).is(':checked'))
{
// Change background image
$('.image-choices-choice-image-wrap').css("background-image", "url(https://cdn.kimkim.com/files/a/content_articles/featured_photos/7a4f69e48562c9adbee4f090033454a8ab23c135/medium-e5cbb5b7a25c93f53245e256729efff8.jpg)");
}
});
</script>
Don't forget to add jQuery if you already haven't
Live Demo: https://jsfiddle.net/jvh6xvzs/12/
The generic form is:
input + label { /* css for unchecked state */ }
input:checked + label { /* css for checked state */ }
input { display: none; }
input {
display: none;
}
input + label { background-color: red; }
input:checked + label { background-color: black; color: white ; }
<input id=option1 type=radio name=dd checked><label for=option1>select</label>
<input id=option2 type=radio name=dd><label for=option2>select 2</label>
I ended up using the guide from:
https://css-tricks.com/override-inline-styles-with-css/
The plugin added the class .image-choices-choice-selected when the radio button was checked. Therefore i used following code to do the trick:
.image-choices-choice-selected #label_1_2_2 span[style] {
background-image:url(http://newimage1.com) !important;
}
Thanks for the suggestions, I guess if this class was not added, I could have used jQuery.

Is it possible to set special styles to checkboxes when all of them are unchecked using only CSS?

It's simple to apply CSS to checked/unchecked checkboxes (using :checked) and their labels. However, I need to apply another style to all checkboxes when all of them are unchecked.
This is relatively simple to implement using JavaScript but I have a widget which I'm not eager to modify so I'd like to know whether there's a CSS trick for that. I suspect that there isn't, but there's always somebody who's smarter :)
PS well, the html bit looks like this, nothing special:
<div>
<input type=checkbox id=chkFilterMath>
<span><label for=chkFilterMath>Math</label></span>
<input type=checkbox id=chkFilterHist>
<span><label for=chkFilterHist>History</label></span>
...
</div>
Current CSS uses the
input:not(:checked) + span label
selector to apply the styles to unchecked checkboxes/labels.
A simplified example may be found here: https://jsfiddle.net/k56hz8va/ I'd like to set color: black to the labels when all of checkboxes are unchecked.
Lasciate ogni speranza, voi ch’entrate
No. There are no such CSS selectors that allows to select previous DOM elements in dependence on state of following elements. See Is there a “previous sibling” CSS selector? and Is there a CSS parent selector? posts for details.
There is a hack around this that I use:
Hide the input itself, but keep the label. Then use the pseudo element ::before to insert some icon to denote checked / unchecked.
Here's a demo: https://codepen.io/anon/pen/WdrdPE
and the code:
<input id="option_1" type="checkbox"><label for="option_1">thing 1</label>
<input id="option_2" type="checkbox"><label for="option_2">thing 2</label>
<input id="option_3" type="checkbox"><label for="option_3">thing 3</label>
css:
#import url('https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
input {
display:none;
}
label {
display:block;
}
input:checked + label {
color:red;
}
label:before {
content:"\f1db";
margin-right:.3em;
font-family:Fontawesome;
}
input:checked + label:before {
content:"\f058";
margin-right:.3em;
font-family:Fontawesome;
}
The #sashaikevich's idea is great but requires some work to solve your question. You could place the labels after all inputs. Then your CSS and HTML will be bulky, but you will be able to control styles of the labels in dependence on all inputs state.
Try to run the snippet below. The latest rule has highest priority, therefore if any (at least one) of checkboxes is checked, then the labels is black. Otherwise the labels is red.
[type=checkbox] {
display: none;
}
#check-box-1:checked~[for=check-box-1] .glyphicon-unchecked,
#check-box-2:checked~[for=check-box-2] .glyphicon-unchecked,
#check-box-3:checked~[for=check-box-3] .glyphicon-unchecked,
#check-box-1:not(:checked)~[for=check-box-1] .glyphicon-check,
#check-box-2:not(:checked)~[for=check-box-2] .glyphicon-check,
#check-box-3:not(:checked)~[for=check-box-3] .glyphicon-check
{
display: none;
}
[for] {
color: red;
}
[type=checkbox]:checked~[for] {
color: inherit;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="check-box-set">
<input id="check-box-1" type="checkbox" />
<input id="check-box-2" type="checkbox" checked="checked" />
<input id="check-box-3" type="checkbox" />
<label for="check-box-1">
<span class="glyphicon glyphicon-unchecked" aira-hidden="true"></span>
<span class="glyphicon glyphicon-check" aira-hidden="true"></span>
1
</label>
<label for="check-box-2">
<span class="glyphicon glyphicon-unchecked" aira-hidden="true"></span>
<span class="glyphicon glyphicon-check" aira-hidden="true"></span>
2
</label>
<label for="check-box-3">
<span class="glyphicon glyphicon-unchecked" aira-hidden="true"></span>
<span class="glyphicon glyphicon-check" aira-hidden="true"></span>
3
</label>
</div>
In the example I use Bootstrap Glyphicons. But it is possible to use another glyps, images or CSS shapes.

label element always enabled

i use disabled attribute for disable/enable elements.but label element has problem with this attribute ,when set this attribute to disabled do not work.
See bellow code & link
<label disabled="disabled" for="ch1">Click to Me</label>
<button disabled="disabled" >Cannot Click to Me</button>
<input type="checkbox" id="ch1" />
http://jsbin.com/wagoku/1/
You can't disable a label per se. Disable the form control it is associated with instead.
<label for="ch1">Click to Me</label>
<input type="checkbox" disabled id="ch1">
http://jsbin.com/pojojike/1/edit?html,output
You can't disable the label use css something like
.disabled {
color: darkgrey;
background-color: grey;
}
And to add the class to your element:
$('#click').addClass('disabled');