Updating the CSS style of elements based on a radio buttons - javascript

I have coded 10 radio buttons which work essentially like a star rating. What I want to do is have another 10 button-like circular elements which update based on what radio button is pushed. For example, if the 9th radio button is pushed, I want only the first button-like circular element to get highlighted (change it's css class). Or if the 5th button is pushed, then I want the 5th circular element to update.
I've looked all over for something similar like maybe using a value to display stars but I can't seem to find anything for what I specifically want to do. I would appreciate all help.
Basically my radio buttons look like this (with ten buttons in the actual code but condensed to three buttons on here):
<div class="points">
<span><input type="radio" name="amount_offered" id="point3" value="3">
<label for="point3">3</label></span>
<span><input type="radio" name="amount_offered" id="point2" value="2">
<label for="point2">2</label></span>
<span><input type="radio" name="amount_offered" id="point1" value="1">
<label for="point1">1</label></span>
</div>
The other circular buttons look like this:
<div class="pointskeep">
<span><id="point3keep"><label>3</label></span>
<span><id="point2keep"><label>2</label></span>
<span><id="point1keep"><label>1</label></span>
</div>
My css code looks like:
.points span label {
border-radius:50%;
color:black;
background:white;
}
.points span:hover ~ span label,
.points span:hover label,
.points span.checked label,
.points span.checked ~ span label {
background:goldenrod;
color:white;
}
.pointskeep span label {
border-radius:50%;
background:goldenrod;
color:white;
}
.pointskeep span.checked label,
.pointskeep span.checked ~ span label {
color:black;
background:white;
}
My js looks like this:
$(document).ready(function(){
// Check Radio-box
$(".points input:radio").attr("checked", false);
$('.points input').click(function () {
$(".points span").removeClass('checked');
$(".nopoints span").removeClass('checked');
$(this).parent().addClass('checked');
});
I've been fiddling a lot with the javascript but I just can't seem to figure out how to update the non-radio elements to their "checked" css style, when the appropriate radio element is clicked. Is there a better approach?

Try this:-
$(document).ready(function () {
$('input').click(function () {
$('input:not(:checked)').parent().removeClass("checked");
$('input:checked').parent().addClass("checked");
});
});
.points span label {
border-radius:50%;
color:black;
background:white;
}
.points span:hover ~ span label,
.points span:hover label,
.points span.checked label,
.points span.checked ~ span label {
background:goldenrod;
color:white;
}
.pointskeep span label {
border-radius:50%;
background:goldenrod;
color:white;
}
.pointskeep span.checked label,
.pointskeep span.checked ~ span label {
color:black;
background:white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="points">
<span><input type="radio" name="amount_offered" id="point3" value="3"/>
<label for="point3">3</label></span>
<span><input type="radio" name="amount_offered" id="point2" value="2"/>
<label for="point2">2</label></span>
<span><input type="radio" name="amount_offered" id="point1" value="1"/>
<label for="point1">1</label></span>
</div>
<div class="pointskeep">
<span><id="point3keep"><label>3</label></span>
<span><id="point2keep"><label>2</label></span>
<span><id="point1keep"><label>1</label></span>
</div>

This is how would go about it.
If a user clicks the button I would get the value of the input
Get all the non button.
Compare index with value
add class accordingly
$(document).ready(function () {
$('input').click(function () {
$('input:not(:checked)').parent().removeClass("checked");
$('input:checked').parent().addClass("checked");
let val = parseInt($('input[name=amount_offered]:checked').val());
$('.point').each(function(index,element ){
if(index+1>val)
$( element ).addClass('checked');
else
$( element ).removeClass('checked');
});
});
});
.points span label {
border-radius:50%;
color:black;
background:white;
}
.points span:hover ~ span label,
.points span:hover label,
.points span.checked label,
.points span.checked ~ span label {
background:goldenrod;
color:white;
}
.pointskeep .checked{
padding: 5px;
border-radius: 50%;
background:goldenrod;
color:white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="points">
<span><input type="radio" name="amount_offered" id="point3" value="3"/>
<label for="point3">3</label></span>
<span><input type="radio" name="amount_offered" id="point2" value="2"/>
<label for="point2">2</label></span>
<span><input type="radio" name="amount_offered" id="point1" value="1"/>
<label for="point1">1</label></span>
</div>
<div class="pointskeep">
<span id="point3keep" class="point"><label>3</label></span>
<span id="point2keep" class="point"><label>2</label></span>
<span id="point1keep" class="point"><label>1</label></span>
</div>

Related

How to uncheck a checkbox when another one is checked?

With Javascript, I would like one first checkbox to get unchecked when I check the second one. I also would like that the two checkboxes can be unchecked after having been checked.
Here is my HTML code :
<input type="checkbox" id="radio-1" class="radio" /><label for="radio-1">Yes</label>
<input type="checkbox" id="radio-2" class="radio" /><label for="radio-2">No</label>
I tried with two checkboxes, but I don't manage to uncheck #1 when #2 is checked.
Here is my Jsfiddle with the example with two checkboxes:
http://jsfiddle.net/3f66j30y/
I also tried with two radio buttons, but I don't manage to remain them unchecked after having been checked.
Add an onclick event to each checkbox to uncheck the other checkbox
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label
{
padding:10px 10px;
text-align:center;
background:#dedede;
color:black;
height: 20px;
width: 100px;
display:inline-block;
}
input[type="checkbox"]:checked + label
{
padding:10px 10px;
text-align:center;
background:green;
color:white;
height: 20px;
width: 100px;
display:inline-block;
}
<input type="checkbox" id="radio-1" class="radio" onclick="document.getElementById('radio-2').checked = false"/><label for="radio-1">Yes</label>
<input type="checkbox" id="radio-2" class="radio" onclick="document.getElementById('radio-1').checked = false"/><label for="radio-2">No</label>
The following in a Vanilla JS solution, which:
binds change events when the DOM is loaded
only binds one change event to the parent container
in the change event, decides what the target is and turns off other checkboxes
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('.select-group').onchange = changeEventHandler;
}, false);
function changeEventHandler(e) {
var cbs = document.querySelectorAll('.cb');
cbs.forEach(function(cb) {
if (cb != e.target)
cb.checked = false;
});
}
input[type="checkbox"] {
display: none;
}
label {
padding: 10px 10px;
text-align: center;
background: #dedede;
color: black;
height: 20px;
width: 100px;
display: inline-block;
}
input[type="checkbox"]:checked+label {
padding: 10px 10px;
text-align: center;
background: green;
color: white;
height: 20px;
width: 100px;
display: inline-block;
}
<div class="select-group">
<input id="cb_yes" type="checkbox" value="yes" class="cb" />
<label for="cb_yes">Yes</label>
<input id="cb_no" type="checkbox" value="no" class="cb" />
<label for="cb_no">No</label>
</div>
It can certainly be improved; after all, one obvious point is that you're searching the DOM for the checkboxes every time they change — you could easily cache them. However, this should serve a point and show you how easy it is to work with standard JS.
Use jQuery to achieve this.
$(".radio").change(function() {
var checked = $(this).is(':checked');
$(".radio").prop('checked',false);
if(checked) {
$(this).prop('checked',true);
}
});
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label
{
padding:10px 10px;
text-align:center;
background:#dedede;
color:black;
height: 20px;
width: 100px;
display:inline-block;
}
input[type="checkbox"]:checked + label
{
padding:10px 10px;
text-align:center;
background:green;
color:white;
height: 20px;
width: 100px;
display:inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="radio-1" class="radio" /><label for="radio-1">Yes</label>
<input type="checkbox" id="radio-2" class="radio" /><label for="radio-2">No</label>
Behavior you are looking for is specific to radio buttons. However the problem is - you can't uncheck it, once checked. In this case you can use three radio buttons - yes, no and none (-) - since clearly you want more then two options:
<input type="radio" id="radio-0" name="group-one" class="radio" checked /><label for="radio-0">-</label><br>
<input type="radio" id="radio-1" name="group-one" class="radio" /><label for="radio-1">Yes</label><br>
<input type="radio" id="radio-2" name="group-one" class="radio" /><label for="radio-2">No</label>
If you prefer to stick with two, you can use your checkboxes with a bit of JavaScript to switch the opposite box off:
function radioSwitch(opposite) {
document.getElementById(opposite).checked = false;
}
document.getElementById("radio-1").addEventListener("click",
function() { radioSwitch("radio-2"); });
document.getElementById("radio-2").addEventListener("click",
function() { radioSwitch("radio-1"); });
<input type="checkbox" id="radio-1" class="radio" /><label for="radio-1">Yes</label>
<input type="checkbox" id="radio-2" class="radio" /><label for="radio-2">No</label>

How do I run a animation on every input change?

I'm working on a animation that appears if you interact with some input elements. On a input change event, the script adds the class highlight and removes it after a delay of 1 second.
The problem is, If I switch the radio buttons faster than 1 second the animation does not work. What options do I have to run the animation on every input change?
$(':radio[name="group1"], :radio[name="group2"]').on('change', function(e) {
$('#' + this.getAttribute('name') + ' .selected').text(this.value);
});
//
// This to get the checked radios
//
$(':radio[name="group1"]:checked, :radio[name="group2"]:checked').trigger('change');
$("div").bind('DOMNodeInserted DOMNodeRemoved', function() {
$(this).addClass('highlight').delay(1000).queue(function(next){
$(this).removeClass("highlight");
next();
});
});
input{
display: none;
}
label{
display: inline-block;
width:20%;
cursor: pointer;
padding: 20px;
margin: 10px;
background: #eee;
color: #111;
}
input:checked + label{
background: #111;
color: #fff;
}
div{
background: #eee;
color: #111;
padding: 10px;
margin: 4px;
}
div span{
display: block;
margin: 4px;
}
.title{
font-weight: bold;
}
div.highlight {
-webkit-animation: highlight 1s infinite;
-moz-animation: highlight 1s;
-o-animation: highlight 1s;
animation: highlight 1s;
}
#-webkit-keyframes highlight {
from {background-color: #0099cc;}
to {background-color: none;}
}
#-moz-keyframes highlight {
from {background-color: #0099cc;}
to {background-color: none;}
}
#-o-keyframes highlight {
from {background-color: #0099cc;}
to {background-color: none;}
}
#keyframes highlight {
from {background-color: #0099cc;}
to {background-color: none;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="a" type="radio" name="group1" value="Description group1 1" checked>
<label for="a">Group1 Radio1</label>
<input id="b" type="radio" name="group1" value="Description group1 2">
<label for="b">Group1 Radio2</label>
<input id="c" type="radio" name="group1" value="Description group1 3">
<label for="c">Group1 Radio3</label>
<input id="d" type="radio" name="group2" value="Description group2 1" checked>
<label for="d">Group2 Radio1</label>
<input id="e" type="radio" name="group2" value="Description group2 2">
<label for="e">Group2 Radio2</label>
<input id="f" type="radio" name="group2" value="Description group2 3">
<label for="f">Group2 Radio3</label>
<!-- Output value here -->
<div id="group1">
<span class="title">Group1</span>
<span class="selected"></span>
</div>
<div id="group2">
<span class="title">Group2</span>
<span class="selected"></span>
</div>
Queue them in the opposite direction:
$("div").bind('DOMNodeInserted DOMNodeRemoved', function() {
$(this).removeClass('highlight').delay(10).queue(function(next) {
$(this).addClass("highlight");
next();
});
});
(and set animetion loop to once)

Expandable list checkboxes disappear after collapse all

I have a form in a javascript expandable list. In several of the headers I have checkboxes and radio buttons. I also have an expand all and collapse all button. My problem is, after clicking the collapse all button, if I click on a list item the checkbox doesn't show up. If I click on expand all button everything shows up normally, but then I have to click on the headers to collapse them down again. Here's a fiddle that reproduces the problem.
Edit to clarify: The problem only occurs after clicking on collapse all, and then clicking on the list header. The child headers no longer show the checkboxes.
HTML:
<body>
<div id="listContainer">
<div class="listControl">
<a id="expandList">Expand All</a>
<a id="collapseList">Collapse All</a>
</div>
<form id="ColForm" action="Table.php" method="post"> <!--Organized list of collumns and filter options-->
<ul id="expList">
<li>Section heading
<ul>
<li><input type="checkbox" name="ColSelect" value="Name" form="ColForm"> <!--If checked, collumn will be included in final table--> Name
<ul>
<li>
<input type="text" name="Name" form="ColForm"><br> <!--filter parameter input-->
</li>
</ul>
</li>
<li><input type="checkbox" name="ColSelect" value="RA,Dec" form="ColForm">Another collumn
<ul>
<li>
<input type="radio" name="PoSearch" value="Range" form="ColForm">Radio button to select form type for this section<br>
<i>I have an option here
<input type="radio" name="Degs" value="Dec" form="ColForm">Option 1
<input type="radio" name="Degs" value="Hex" form="ColForm">Option 2</i><br>
Text input 1<br>
<input type="text" name="RA" form="ColForm">deg<br>
Text input 2<br>
<input type="text" name="Dec" form="ColForm">deg<br>
<input type="radio" name="PoSearch" value="Area" form="ColForm">Second form option<br>
<i>Text input A</i><br>
<input type="text" name="Area" form="ColForm"><br>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<input type="submit" value="submit" form="ColForm">
</form>
</div>
</body>
CSS:
#expList ul, li {
list-style: none;
margin:0;
padding:0;
cursor: pointer;
}
#expList p {
margin:0;
display:block;
}
#expList p:hover {
background-color:#121212;
}
#expList li {
line-height:140%;
text-indent:0px;
background-position: 1px 8px;
padding-left: 20px;
background-repeat: no-repeat;
}
/* Collapsed state for list element */
#expList .collapsed {
}
/* Expanded state for list element
/* NOTE: This class must be located UNDER the collapsed one */
#expList .expanded {
}
#expList {
clear: both;
}
.listControl{
margin-bottom: 15px;
}
.listControl a {
border: 1px solid #555555;
color: #555555;
cursor: pointer;
height: 1.5em;
line-height: 1.5em;
margin-right: 5px;
padding: 4px 10px;
}
.listControl a:hover {
background-color:#555555;
color:#222222;
font-weight:normal;
}
Javascript:
function prepareList() {
$('#expList').find('li:has(ul)')
.click( function(event) {
if (this == event.target) {
$(this).toggleClass('expanded');
$(this).children('ul').toggle('medium');
}
//return false;
})
.addClass('collapsed')
.children('ul').hide();
//Create the button functionality
$('#expandList')
.unbind('click')
.click( function() {
$('.collapsed').addClass('expanded');
$('.collapsed').children().show('medium');
})
$('#collapseList')
.unbind('click')
.click( function() {
$('.collapsed').removeClass('expanded');
$('.collapsed').children().hide('medium');
})
};
$(document).ready( function() {
prepareList()
});

"X" check box with an input box

I am attempting to create a check box with an X instead of a check using an input box. However, some I want to work as a radio button (when you click one, the other's get "un-checked").
Basically, a group of three check boxes that only allows 1 box to have the check in it at a time.
Does anyone know an easy way to accomplish the radio button-esq approach to this without creating a specific function for each group of check box's?
HTML
<input name="box1" id="box1" class="checkBox" value="" readonly="readonly" onclick="return checkBox('box1')">
CSS
.checkBox { background-color:#fff; margin: 0; border: 0; padding: 0; border:1px solid #000; text-align: center; cursor: default;font-family: 'Arial Narrow', Arial, sans-serif;font-size:11px;font-weight:bold;width:1.1em;height:1.1em; }
Function
function checkBox(box) {
x = document.getElementById(box).value;
document.getElementById(box).value = (x == "X") ? "" : "X";
}
You can use custom radio buttons (css only) that looks like checkbox (Demo on jsBin and Demo on jsFiddle)
CSS:
div.radios > label > input {
visibility: hidden;
}
div.radios > label {
display: inline-block;
margin: 0 0 0 -10px;
padding: 0 0 10px 0;
height: 20px;
cursor:pointer;
}
div.radios > label > img {
display: inline-block;
padding: 0px;
height:20px;
width:20px;
background: none;
vertical-align:top;
}
div.radios > label > input:checked +img {
background: url(https://cdn2.iconfinder.com/data/icons/picons-essentials/71/no-24.png);
background-repeat: no-repeat;
background-position:center center;
background-size:20px 20px;
}
HTML:
<div class='radios'>
<label title="item1">
<input type="radio" name="foo" value="0" /> <img /> Radio One
</label>
<label title="item2">
<input type="radio" name="foo" value="1" /> <img /> Radio Two
</label>
</div>
You can give the radio group an identifier class, for instance "radio" and onclick reset them and set val of the clicked one. A jquery sample would be
<input class="checkBox radio" value="" readonly="readonly">
<input class="checkBox radio" value="" readonly="readonly">
<input class="checkBox radio" value="" readonly="readonly">
$(".radio").click(function() {
$(".radio").val('');
$(this).val('X');
});
For a pure CSS solution (that actually validates), you could use something like:
<input id="rd1" type="radio" name="opt" /><label for="rd1"></label>
<input id="rd2" type="radio" name="opt" /><label for="rd2"></label>
<input id="rd3" type="radio" name="opt" /><label for="rd3"></label>
And this is the CSS for it:
.radio-special {
display: none;
}
.radio-special + label {
background: #ddd;
height:24px;
width: 24px;
box-shadow: inset 0 0 2px 2px #aaa;
display: inline-block;
}
.radio-special:checked + label {
background: url('https://cdn1.iconfinder.com/data/icons/30_Free_Black_ToolBar_Icons/20/Black_Remove.png') #ddd no-repeat 2px 2px;
}
Note that this will still look a bit weird in the html side of it, but at least its valid markup.
Check how that displays on older versions of IE. It works fine on IE10.
Fiddle
Thanks for everyone's help! I've taken everyone's advice and decided to use a custom image radio/check box through css.
This method will not work for IE7/8 because of the :checked attribute but all you need to do is use selectivizr and everything should run smoothly.
HTML
<input id="option_1" name="option1" type="radio">
<label for="option_1">Option 1</label>
<input id="option_2" name="option2" type="radio">
<label for="option_2">Option 2</label>
<input id="option_3" name="option3" type="radio">
<label for="option_3">Option 3</label>
CSS
input[type='checkbox'], input[type='radio'] { opacity: 0; float: left; width: 14px; }
input[type='radio'] + label, input[type='checkbox'] + label {
margin: 0;
margin-right:-10px; /* Position between the box+label */
clear: none;
padding: 1px 1px 1px 20px; /* Position of the box+label */
cursor: pointer;
background: url('emptyBox.png') left center no-repeat;
float:left;
}
input[type='radio']:checked + label, input[type='checkbox']:checked + label {
background-image: url('selectedBox.png');
}

How to create radio button as buttons?

How to create regular radio buttons to like normal buttons styled with css?
Here are my radio buttons:
<div class="radio-toolbar">
<input type="radio" id="radio1" name="radios" value="all" checked>
<label for="radio1">Radio1</label>
<input type="radio" id="radio2" name="radios"value="false">
<label for="radio2">Radio2</label>
<input type="radio" id="radio3" name="radios" value="true">
<label for="radio3">Radio3</label>
</div>
And my css:
.radio-toolbar {width:410px;}
.radio-toolbar input[type="radio"] {
display:none;
}
.radio-toolbar label {
display:inline-block;
background:#FFF;
font-family:"Arial Black", sans-serif;
font-size:12px;
color:#666666;
width:106px;
padding-left:4px;
}
.radio-toolbar [type="radio"]:checked + label {
background:url("../images/radiochecked.png") no-repeat;
color:#FFF;
}
.radio1 [type="radio"]:checked + label {
background:url("../images/radio1.png") no-repeat;
color:#FFF;
}
.radio2 input[type="radio"]:checked + label {
background:url("../images/radio2.png") no-repeat;
color:#FFF;
}
.radio-toolbar label:hover {background-color:#bbb;
background:url("../images/radiohover.png") no-repeat;
color:#FFF;
}
.radio2 label:hover {background-color:#bbb;
}
It involves hiding the radio button element and putting in a different element instead (at runtime, to maintain backward compatibility with non-JS-enabled browsers), then echoing changes to the new element to the hidden radio button's state.
As you're already using jQuery, you might consider jQuery UI, which has exactly this functionality.

Categories

Resources