Styling Custom Checkbox with Image Not Appearing - html

I'm trying out HTML and CSS and am relatively new to the entire concept. I'm currently working on styling a custom checkbox using an image I made from Photoshop. I am not able to figure out why my image is not appearing when I set it this way.
HTML
<ul id="myUL" class="ulChecklist">
<form action="/action_page.php">
<li><input type="checkbox" name="instruction">
<label for="Step1">Step 1</label>
</li>
<li><input type="checkbox" name="instruction">
<label for="Step2">Step 2</label>
</li>
<li><input type="checkbox" name="instruction">
<label for="Step3">Step 3</label>
</li>
</form>
</ul>
CSS
input[type="checkbox"] {
opacity: 0;
}
input[type="checkbox"] + label {
background: url(check.png) left center no-repeat;
}
This is the pre-checked image I want to add.
This is the post-checked image I want to add.
As you can see, it isn't appearing.
Is something wrong with the way I write these codes? I've checked the following Lynda course link: https://www.lynda.com/HTML-tutorials/Styling-radio-buttons-check-boxes-images/612196/646907-4.html
But it isn't working out for me. I would greatly appreciate help from people! Thank you for taking your time to answer a noob's question!

Try this.
ul{
list-style:none;
}
input[type="checkbox"] {
opacity: 0;
position: absolute;
width: 100%;
height: 100%;
z-index: -1;
}
input[type="checkbox"] + label {
background: url(https://i.stack.imgur.com/eiFBl.png) no-repeat 0 center;
padding-left:60px;
line-height:50px;
display: inline-block;
cursor:pointer;
}
input[type="checkbox"]:checked + label {
background: url(https://i.stack.imgur.com/mCst2.png) no-repeat 0 center;
}
.check-wrap{
position:relative;
display: inline-block;
}
<ul id="myUL" class="ulChecklist">
<form action="/action_page.php">
<li>
<div class="check-wrap">
<input type="checkbox" name="instruction" id="Step1">
<label for="Step1">Step 1</label>
</div>
</li>
<li>
<div class="check-wrap">
<input type="checkbox" name="instruction" id="Step2">
<label for="Step2">Step 2</label>
</div>
</li>
<li>
<div class="check-wrap">
<input type="checkbox" name="instruction" id="Step3">
<label for="Step3">Step 3</label>
</div>
</li>
</form>
</ul>

You are on the right path. Just that you need to resize the background image and float it to the left. And one of the most important part is to associate the label with the input checkbox with for and id:
input[type="checkbox"] {
opacity: 0;
}
input[type="checkbox"] + label {
background: url(http://www.iconarchive.com/download/i86039/graphicloads/100-flat-2/check-1.ico) left center no-repeat;
float: left;
padding-left: 25px; /*image width plus extra padding */
background-size: 20px 20px;
}
input[type="checkbox"]:checked + label {
background: url(https://d30y9cdsu7xlg0.cloudfront.net/png/2181-200.png) left center no-repeat;
float: left;
padding-left: 25px; /*image width plus extra padding */
background-size: 20px 20px;
}
<ul id="myUL" class="ulChecklist">
<form action="/action_page.php">
<li><input id="Step1" type="checkbox" name="instruction">
<label for="Step1">Step 1</label>
</li>
<li><input id="Step2" type="checkbox" name="instruction">
<label for="Step2">Step 2</label>
</li>
<li><input id="Step3" type="checkbox" name="instruction">
<label for="Step3">Step 3</label>
</li>
</form>
</ul>

The code you have provided makes no provision for the background label image when the adjacent sibling pseudo-state is :checked.
You'll need to account for both states, e.g: input[type="checkbox"] & input[type="checkbox"]:checked
input[type="checkbox"] + label {
background: url(check.png) left center no-repeat;
}
input[type="checkbox"]:checked + label {
background: url(check-alt.png) left center no-repeat;
}
Edit
You may also need to declare background size properties.

Sorry I am not able to view the Lynda course as I am not a member, but will do my best to answer this.
If I were setting up I would make the following changes to your code:
<ul id="myUL" class="ulChecklist">
<form action="/action_page.php">
<li><input type="checkbox" name="instruction">
<label for="Step1">Step 1</label>
<img src=“./check.png” class=“checkmark-section”>
</li>
</form>
</ul>
Then in the CSS I would callout the checkmark-section class and add in the click effect image under the pseudo class of focus < This is the css word for clicked.
For example:
.checkmark-section:focus {
background: url(./checkmark-active)
}
This will mean that once checkmark-active is clicked, it will swap over to show the depressed check mark image instead. I have not tried this out, but that is how I would expect it to work.
All the best,
Dan

Related

How do I get label with checkbox to work?

I made a label because I wanted to apply css to the checkbox.
However, the label is not working except for the checkbox whose id is 'checkall'.
<div class="divCenter cartDiv">
<ul>
<li class="selectallforcart">
<div class="hanadiv">
<input type="checkbox" id="checkall" class="check" value="0"/>
<label for="checkall"></label> selelct All
</div>
</li>
</ul>
<% ArrayList<LikePdVO> likeList = (ArrayList<LikePdVO>)(request.getAttribute("likeList"));
for(LikePdVO vo : likeList) {
String arr[] = vo.getPhoto().split("\\*");
%>
<div class="cartProduct" id="<%=vo.getPd_id()%>">
<div class="fl cartCheck">
<input type="checkbox" class="check" name="<%=vo.getName()%>"
value="<%=vo.getOrder_price()%>" id="<%=vo.getPd_id()%>">
<label for="<%=vo.getPd_id()%>">
</label>
</div>
this is my code.
.cartDiv input[type=checkbox] + label {
display: inline-block;
width: 20px;
height: 20px;
border: 2px solid #bcbcbc;
cursor: pointer;
}
.cartDiv input[type=checkbox]:checked + label {
background-color: #866744;
}
.cartDiv input[type=checkbox] {
display: none;
}
And this is CSS.
When you mean that the label is not working, I assume you mean that clicking on it doesn't cause the checkbox to be checked?
Either way, the label's text needs to be within the element, so it should be:
<label for="checkall">Select all</label>
And your other labels contain no text, so the label itself is probably quite small, despite the CSS, and therefore not clickable.
You need to set label text in the label tag not out side of it.
<div class="divCenter cartDiv">
<ul>
<li class="selectallforcart">
<div class="hanadiv">
<input type="checkbox" id="checkall" class="check" value="0" />
<label for="checkall">selelct All</label>
</div>
</li>
</ul>
</div>

Display Prompt window when hovering over the "a" (link) tag

Is there any way to display a prompt window when hovering over the "a" link tag? I've shown display contents over hover, however the prompt isn't showing when hovering.
.prompt {
display:none;
}
.prompt .description-box:hover {
display: contents;
}
<button class="accordion">Catagories</button>
<ul class="accordion-content">
<li>
<label>
<input type="checkbox" name="checkbox" >Item1
</label>
<a class="description-box"href="#">ℹ️</a>
<p class="prompt" >This is a prompt</p>
</li>
<li>
<label>
<input type="checkbox" name="checkbox" />Item2
</label>
</li>
<li>
<label>
<input type="checkbox" name="checkbox" />Item3
</label>
</li>
</ul>
You could do this using the onmouseover event built into html tags.
Add an index.js file
function displayPrompt() {
document.querySelector(".prompt").style.display = "contents"
}
<button class="accordion">Categories</button>
<ul class="accordion-content">
<li>
<label>
<input type="checkbox" name="checkbox" >Item1
</label>
<a class="description-box" "href="#" onmouseover="displayPrompt()">ℹ️</a>
<p class="prompt">This is a prompt</p>
</li>
<li>
<label>
<input type="checkbox" name="checkbox" />Item2
</label>
</li>
<li>
<label>
<input type="checkbox" name="checkbox" />Item3
</label>
</li>
</ul>
<script src="./index.js"></script>
.prompt {
display: none;
}
That's one way of doing
||||||||||||||||||
You could also do it by adding a class to the element by modifying your index.js like so:
function displayPrompt() {
document.querySelector(".prompt").classList.add(".show")
}
And a 'show' class to your css file
.prompt {
display: none;
}
.prompt.show {
display: contents
}
I personally prefer the second way because you don't edit your styles directly from your javascript
Non-JavaScript Option 1: Re-order your CSS and place your prompt inside your anchor element.
.description-box:hover .prompt {
display: contents;
}
And the HTML:
<a class="description-box" href="#">ℹ️<p class="prompt">This is a prompt</p></a>
Non-JavaScript Option 2: Create your own prompt with styling, as demonstrated (very simply) with the second li in my snippet.
Snippet that includes both of those non-JavaScript options:
.prompt {
display: none;
}
/* Swap the classes */
.description-box:hover .prompt {
display: contents;
}
/* Some extra stuff for syling demo */
.promptNEW {
position: absolute;
top: 40vh;
left: 30vw;
background-color: #ddd;
border: 1px solid black;
width: 30vw;
height: 40vh;
content: "prompt content";
display: none;
}
.description-box:hover .promptNEW {
display: block;
}
<button class="accordion">Catagories</button>
<ul class="accordion-content">
<li>
<label><input type="checkbox" name="checkbox" >Item1</label>
<!-- Put the prompt INSIDE the 'a' element -->
<a class="description-box" href="#">ℹ️
<p class="prompt" >This is a prompt</p>
</a>
</li>
<!-- Extra to demonstrate another option -->
<li>
<label><input type="checkbox" name="checkbox" >ItemNEW</label>
<a class="description-box" href="#">ℹ️
<p class="promptNEW" >This is a prompt</p>
</a>
</li>
<li>
<label><input type="checkbox" name="checkbox" />Item2</label>
</li>
<li>
<label><input type="checkbox" name="checkbox" />Item3</label>
</li>
</ul>
Use JavaScript. Just Trigger the function with an onmouseover event. You can see an example below. Hope this solves your problem.
function a() {
prompt('What is your name? ')
}
<a onmouseover="a()" href="#">Link</a>
You can solve the problem by creating a div of the link.
.description-box a p {display:none;}
.description-box:hover p{
display: block;
}
<button class="accordion">Catagories</button>
<ul class="accordion-content">
<li>
<label>
<input type="checkbox" name="checkbox" >Item1
</label><div class="description-box">
<a href="">ℹ️
<p>This is a prompt</p></a></div>
</li>
<li>
<label>
<input type="checkbox" name="checkbox" />Item2
</label>
</li>
<li>
<label>
<input type="checkbox" name="checkbox" />Item3
</label>
</li>
</ul>
Took help from
How to make text appear when hover over a href
You can use opacity and visibility instead to create a soft fading effect on hover. You can also customize the .prompt as per your needs.
.description-box {
position: relative;
display: inline-block;
}
.description-box .prompt {
visibility: hidden;
width: 120px;
color: #000;
text-align: center;
position: absolute;
bottom: -2%;
left: 125%;
opacity: 0;
transition: opacity 0.3s;
}
.description-box:hover .prompt {
visibility: visible;
opacity: 1;
}
<button class="accordion">Catagories</button>
<ul class="accordion-content">
<li>
<label>
<input type="checkbox" name="checkbox" >Item1
</label>
<a class="description-box" href="#">ℹ️
<span class="prompt">This is a prompt</span>
</a>
</li>
<li>
<label>
<input type="checkbox" name="checkbox" />Item2
</label>
</li>
<li>
<label>
<input type="checkbox" name="checkbox" />Item3
</label>
</li>
</ul>

Checkboxes aligned in a row

Bear with me as I'm not well versed with front end design. How can I get the following checkboxes aligned and stacked neatly as in the picture?
Right now I have them in a row of 9 checkboxes but they're not aligned and are spaced according to the length of the text.
As seen here:
JSFiddle
HTML:
<div class="tags">
<div class="col">
<label>
<input type="checkbox" rel="accounting" />Accounting</label>
<label>
<input type="checkbox" rel="courier" />Courier</label>
<label>
<input type="checkbox" rel="project-management" />Project Management</label>
<label>
<input type="checkbox" rel="video-games" />Video Games</label>
<label>
<input type="checkbox" rel="dentistry" />Dentistry</label>
<label>
<input type="checkbox" rel="librarian" />Librarian</label>
<label>
<input type="checkbox" rel="programmer" />Programmer</label>
<label>
<input type="checkbox" rel="architect" />Architect</label>
</div>
<div class="col">
<label>
<input type="checkbox" rel="photographer" />Photographer</label>
<label>
<input type="checkbox" rel="it" />IT</label>
<label>
<input type="checkbox" rel="artist" />Artist</label>
<label>
<input type="checkbox" rel="web-developer" />Web Developer</label>
<label>
<input type="checkbox" rel="web-designer" />Web Designer</label>
<label>
<input type="checkbox" rel="neurologist" />Neurologist</label>
<label>
<input type="checkbox" rel="veterinarian" />Veterinarian</label>
<label>
<input type="checkbox" rel="teacher" />Teacher</label>
</div>
<div class="col">
<label>
<input type="checkbox" rel="character-animator" />Character Animator</label>
<label>
<input type="checkbox" rel="salesman" />Salesman</label>
<label>
<input type="checkbox" rel="telemarketing" />Telemarketeing</label>
<label>
<input type="checkbox" rel="construction" />Construction</label>
<label>
<input type="checkbox" rel="lawyer" />Lawyer</label>
<label>
<input type="checkbox" rel="actor" />Actor</label>
<label>
<input type="checkbox" rel="policeman" />Policeman</label>
<label>
<input type="checkbox" rel="forestry" />Forestry</label>
</div>
</div>
<!-- end tags -->
CSS:
.filter {
width: 850px;
padding: 25px;
border: 1px solid black;
margin: 25px;
float: left;
}
.col {
width: 100%;
display: block;
margin-right: 10px;
}
label{
vertical-align: top;
float: left;
width: 160px;
}
Edit
I'd use a table layout. Simply add the following CSS:
.tags {
display: table;
}
.col {
display: table-row;
}
.col label {
display: table-cell;
}
Updated fiddle: http://jsfiddle.net/5sz6qdos/13/
Others might use flexbox but I have never used it myself, so not sure how to implement that.
I would probably go a different route, however, to make it more semantic and setup to be more flexible:
<ul class="tags">
<li class="tag"><label>...
....<!--put ALL of your label elements in a single parent element-->
</ul>
and
.tag {
display: block;
float: left;
width: 25%; /*for if you want 4 columns, or*/
width: 120px; /*if you want specific widths*/
}
.tags:after { content: ""; display: table; clear: both; } /*clearfix*/
Using percentages will flex all the columns, or using fixed pixels will allow the elements to flow (my preference).
first of all change your html code like that:
<input type="checkbox" name="accounting" id="accounting" rel="accounting" />
<label for="accounting">Accounting</label>
Do this for every checkbox.
Then Do the follwoing css
.tags{clear:both}
.col{float:left;width:120px;/*adjust your width */}
.col lable, .col input{display:inline-block;}
.col label{width:120px;/*adjust your width */}
.col input{width:20px;/*adjust your width */}
For starters, you're using the label tag wrong. They should not have an input inside them. And how I would do what you're trying to do is something like this:
<div class="col">
<label>Job</label>
<input>
<label>Job</label>
<input>
</div>
<div class="col">
<label>Job</label>
<input>
<label>Job</label>
<input>
</div>
Then in your CSS...
.col {
display: inline-block;
width: 33.3%;
vertical-align: top;}
label {
display: inline-block;
width: 100px;}
input {
display: inline-block;
width: calc(100% - 100px);}
To make sure the columns, labels, and inputs all touch nicely, you have to remove the whitespace. You can either remove it from the code itself, connect elements with comment tags, or set the font-size to 0 on the container. And it wouldn't hurt to throw this in your CSS:
* {
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;}
So you're kinda having the same problem I mentioned belwo... the text can't be what you're basing off of. You have to set an actual width.
http://jsfiddle.net/tueez7bx/6/
This works, but extends past your limits. You should really consider my other options I provide below.
Also, you can't use ids the way you are. That is incorrect. You'll need to change all the #col to .col.
Second Edit
So based on a comment provided below I have come to realize the goal of what you're trying. Sadly because of the flexibility of text and items you would need to have defined widths for each to line up perfectly. If you don't know what the longest string will be then you will have a problem with risking that string breaking below the words or running into the next checkbox.
You can see that here: http://jsfiddle.net/5sz6qdos/22/
However.. if you extend the width of your container and just have a greater width you will be able to avoid this...
You can see what I am imagining you're asking here: http://jsfiddle.net/5sz6qdos/28/
Before Edit
You're doing well overall...
http://jsfiddle.net/5sz6qdos/3/
.col {
width: 160px;
float: left;
margin-right: 10px;
}
label{
display: block;
}
This does define a width and doesn't allow for full flexibility. It does however give you the option of going to responsive layouts with this data.
To get the boxes the way you want them you cannot use pure CSS. You will need a mix of Javascript. Checkboxes themselves can not be edited with CSS directly. You will need to hide the checkbox with display: none and add an image for the boxes themselves. Once you have done this you will need to use jQuery to check and uncheck each box. That is the only way to get the exact look.
SO has a lot of answers out there for the jQuery part.
Thanks to all for the knowledge and push in the right direction.
Here is the working solution I found while setting line widths.
JSFiddle
HTML
<div class="filter">
<h3 style="text-align: center;">Blog Profession Filters</h3>
<ul class="checkbox">
<li>
<input type="checkbox" id="cb1" rel="accounting" value="accounting" />
<label for="cb1">Accounting</label>
</li>
<li>
<input type="checkbox" id="cb2" rel="project-management" value="project-management" />
<label for="cb2">Project Management</label>
</li>
<li>
<input type="checkbox" id="cb3" rel="information technology" value="information-technology" />
<label for="cb3">Information Technology</label>
</li>
<li>
<input type="checkbox" id="cb4" rel="courier" value="courier" />
<label for="cb4">Courier</label>
</li>
<li>
<input type="checkbox" id="cb5" rel="video-games" value="video-games" />
<label for="cb5">Video Games</label>
</li>
<li>
<input type="checkbox" id="cb6" rel="web-development" value="web-development" />
<label for="cb6>">Web Development</label>
</li>
<li>
<input type="checkbox" id="cb7" rel="veterinarian" value="veterinarian" />
<label for="cb6>">Veterinarian</label>
</li>
<li>
<input type="checkbox" id="cb8" rel="web-designer" value="web-designer" />
<label for="cb6>">Web Designer</label>
</li>
<li>
<input type="checkbox" id="cb9" rel="attorney" value="attorney" />
<label for="cb9>">Attorney</label>
</li>
<li>
<input type="checkbox" id="cb10" rel="medical-practitioner" value="medical-practitioner" />
<label for="cb10>">Medical Practitioner</label>
</li>
<li>
<input type="checkbox" id="cb11" rel="telemarketing" value="telemarketing" />
<label for="cb11>">Telemarketing</label>
</li>
<li>
<input type="checkbox" id="cb12" rel="construction" value="construction" />
<label for="cb12>">Construction</label>
</li>
</ul>
</div>
<div id="results">
<ul class="results">
<li class="accounting" style="list-style-type:none"> Accounting
</li>
<li class="project-management" style="list-style-type:none"> Game QA Project Management
</li>
<li class="information-technology" style="list-style-type:none"> Information Technology
</li>
<li class="courier" style="list-style-type:none"> Courier / Parcel Delivery
</li>
<li class="video-games" style="list-style-type:none"> Video Games
</li>
<li class="web-development" style="list-style-type:none"> Web Development
</li>
<li class="veterinarian" style="list-style-type:none"> Veterinarian
</li>
<li class="web-designer" style="list-style-type:none"> Wed Designer
</li>
<li class="attorney" style="list-style-type:none"> Attorney
</li>
<li class="medical-practitioner" style="list-style-type:none"> Medical Practitioner
</li>
<li class="telemarketing" style="list-style-type:none"> Telemarketing
</li>
<li class="construction" style="list-style-type:none"> Construction
</li>
</ul>
</div>
CSS
.filter {
width: 850px;
padding: 25px;
border: 1px solid black;
margin: 25px;
}
ul.checkbox {
margin: 0;
padding: 0;
margin-left: 20px;
list-style: none;
}
ul.checkbox li input {
margin-right: .25em;
}
ul.checkbox li {
border: 1px transparent solid;
display:inline-block;
width:12em;
}
ul.checkbox li label {
margin-left:;
}
ul.checkbox li:hover, ul.checkbox li.focus {
background-color: lightyellow;
border: 1px gray solid;
width: 12em;
}
.results {
width: 850px;
padding: 25px;
border: 1px solid black;
margin: 25px;
}

How to include check boxes in list view using jQuery Mobile?

I need to display the status of every user with three different levels. So I have a list view of all users. I need to display the status of 3 levels (either attend or not). But I am unable to do that. Can any one help me how can I do similar to following image?
I am getting check box below the name. But I need at right corner which I have drawn with blue color.
Code:
<ul data-role="listview" data-filter="true" style="margin-right : 5px;margin-left : 5px" id="progress_list" data-filter-placeholder="Filter Names" >
<li data-icon="false" class="listitem"><a href="#" onClick='#'>Jashwin <input type="checkbox" name="submt_photo" id="submt_photo" class="custom" checked ><input type="checkbox" name="submt_Identity" id="submt_Identity" class="custom" ><input type="checkbox" name="submt_Address" id="submt_Address" class="custom" checked ></a></li>
</ul>
Fiddle:
http://jsfiddle.net/manjunath_r/eh6o90gp/
It will be very appreciate if any one let me know how to give different colors of each check box.
Looks like there's a position: absolute; on .ui-checkbox input, .ui-radio input causing them to stack on top of one another. Here's a version with them side by side http://jsfiddle.net/kdur163h/.
If you have access to the actual markup - you could adjust it like so to have the inputs moved. http://jsfiddle.net/9dygszh4/ ( Wrapped the User's name in a span to float them to the left, because floating the inputs would reverse the order. )
If you want the checkboxes to have the jQM look and feel, you could do something like this:
Here is DEMO showing both options
For a controlgroup look
<ul data-role="listview" data-filter="true" id="progress_list" data-filter-placeholder="Filter Names" class="has-right-radio">
<li data-icon="false" class="listitem">
Jashwin
<div data-role="controlgroup" data-type="horizontal" data-mini="true" class="right-radio">
<label><input type="checkbox" name="submt_photo" id="submt_photo" class="custom" checked /> A</label>
<label><input type="checkbox" name="submt_Identity" id="submt_Identity" class="custom" />B</label>
<label><input type="checkbox" name="submt_Address" id="submt_Address" class="custom" checked />C</label>
</div>
</li>
</ul>
.has-right-radio a {
padding-right: 124px !important;
}
.right-radio {
position: absolute;
top: 0px;
bottom: 0px;
right: 0px;
width: 112px;
}
.right-radio .ui-checkbox input {
visibility: hidden;
}
If you prefer individual checkboxes instead of toggle buttons:
<ul data-role="listview" data-filter="true" id="progress_list2" data-filter-placeholder="Filter Names" class="has-right-radio">
<li data-icon="false" class="listitem">
Jashwin
<div class="right-radio">
<label><input type="checkbox" name="submt_photo" id="submt_photo" class="custom" checked /> </label>
<label><input type="checkbox" name="submt_Identity" id="submt_Identity" class="custom" /> </label>
<label><input type="checkbox" name="submt_Address" id="submt_Address" class="custom" checked /> </label>
</div>
</li>
</ul>
#progress_list2 .has-right-radio a {
padding-right: 132px !important;
}
#progress_list2 .right-radio {
position: absolute;
top: 0px;
bottom: 0px;
right: 0px;
width: 120px;
border: 1px solid rgb(204, 204, 204);
}
#progress_list2 .right-radio .ui-checkbox {
display: inline;
float: left;
padding: 0;
margin: 0;
}
#progress_list2 .right-radio .ui-checkbox label {
padding-right: 0;
padding-left: 36px;
border:0;
border-radius: 0;
}

Clicking on label doesn't check radio button

I'm trying to make a color picker by setting up html like this:
<ol class="kleurenkiezer list-reset clearfix">
<li>
<input type="radio" id="kleur_wit" name="kleurenkiezer" value="wit">
<label for="kleur_wit" style="background: white;"></label>
</li>
<li>
<input type="radio" id="kleur_creme" name="kleurenkiezer" value="creme">
<label for="kleur_creme" style="background: #fffceb;"></label>
</li>
<li>
<input type="radio" id="kleur_lichtbruin" name="kleurenkiezer" value="lichtbruin">
<label for="kleur_lichtbruin" style="background: #968272;"></label>
</li>
<li>
<input type="radio" id="kleur_bordeauxrood" name="kleurenkiezer" value="bordeauxrood">
<label for="kleur_bordeauxrood" style="background: #941514;"></label>
</li>
<li>
<input type="radio" id="kleur_oudgroen" name="kleurenkiezer" value="oudgroen">
<label for="kleur_oudgroen" style="background: #7fa298;"></label>
</li>
<li>
<input type="radio" id="kleur_lichtblauw" name="kleurenkiezer" value="lichtblauw">
<label for="kleur_lichtblauw" style="background: #487eae;"></label>
</li>
<li>
<input type="radio" id="kleur_oudgeel" name="kleurenkiezer" value="oudgeel">
<label for="kleur_oudgeel" style="background: #b79130;"></label>
</li>
<li>
<input type="radio" id="kleur_zwart" name="kleurenkiezer" value="zwart">
<label for="kleur_zwart" style="background: #000;"></label>
</li>
</ol>
What I'm trying to do is make the actual radio button invisible to the user and make the label clickable so that I have a neat list of colored squares that you can select one of. Now my radio button doesn't seem to get checked.. Why would that be?
My css:
.kleurenkiezer {
width: 165px;
margin-left: -10px;
float: right;
}
.kleurenkiezer li {
position: relative;
width: 45px;
height: 45px;
margin: 0 0 10px 10px;
border: 1px solid #bbbbbb;
float: left;
}
.kleurenkiezer li input {
position: absolute;
top: 10px;
left: 10px;
z-index: 1000;
}
.kleurenkiezer li label {
position: absolute;
top: 0;
left: 0;
width: 43px;
height: 43px;
}
New answer for really old question.. :) Not sure it's your case, but I'm experimentind the same exact issue when clicking labels on a page where there are 2 duplicated forms, one of the 2 always hidden. One is used on a page area for mobile devices, the other for desktop devices.
The one appearing first on html flow is working properly, the other no. Fake example, see js fiddle:
<input type="radio" id="value-1" name="sort"/>
<label for="value-1">value 1</label>
<input type="radio" id="value-2" name="sort"/>
<label for="value-2">value 2</label>
<input type="radio" id="value-1" name="sort"/>
<label for="value-1">value 1</label>
<input type="radio" id="value-2" name="sort"/>
<label for="value-2">value 2</label>
https://jsfiddle.net/stratboy/8ua16gm3/1/
So for now, for me, the trick here is to find a way to avoid form duplication.
The radio button work for me.
You can set in css display:none for input checkbox:
.kleurenkiezer input[type=radio] {
display:none
}
For the same thing (a colorpicker) i used another approach and i think it's simpler. Just replace your form with a list of buttons and build 1 function where you pass the color. it's something like this:
<li class="color-box"><button type="button" class="color-btn" style="background-color:#BDC3C7;" onclick="wFontColour('#BDC3C7')"></button></li>
then in your function you do whatever you need to with that colour, in my case it looked like this:
function wFontColour(fontColour) {
document.execCommand("foreColor", false, fontColour);
};
if you want to keep your approach, give your label an id and try this:
$('#myLabel').each('click', function(){
$(this).closest('input:radio').attr('checked', 'checked');
});
Setting an attribute checked won't allow to re checked when you clicked once again. it will work for only once. Give following code a try
$(document).on('click','li label', function(){
$(this).closest('li').find('input:radio').trigger('click');
});
I experienced the same problem as #Luca (and possibly OP) using simple_form_for() in a mobile view and a desktop view. The mobile and desktop inputs had duplicate ids that were causing issues. I ended up using :namespace in the mobile view form:
<%= simple_form_for(:registration, namespace: "mobile") do |f| %>
The namespace generated ids that were unique and my radio buttons worked correctly after that.