So I am a total n00bie here, so please excuse any errors/issues I have made with this or any etiquette faux pas!
I am looking to build some nice, easy to touch buttons for radio inputs on a form that is using field yields. The idea being they are greyscaled at first and upon hover & selection, they lose the greyscale and reveal the colour beneath.
As I am sure you can see, I have hacked around at code I have found on here to try and get this to all work. Despite not having worked with jQuery before, the below code actually functions as I want to too.
I just need the query to apply the .active class to a second element (the label with the class .icon )
Please find below the code I am using.
CSS
input[type='radio']:checked{
positon:absolute;
-webkit-filter: brightness(1.1) grayscale(0) opacity(1)!important;
-moz-filter: brightness(1.1) grayscale(0) opacity(1)!important;
filter: brightness(1.1) grayscale(0) opacity(1)!important;
}
.drinkcard-cc:hover{
-webkit-filter: brightness(1.2) grayscale(.5) opacity(.9);
-moz-filter: brightness(1.2) grayscale(.5) opacity(.9);
filter: brightness(1.2) grayscale(.5) opacity(.9);
}
.drinkcard-cc:checked{
-webkit-filter:grayscale(0);
-moz-filter:grayscale(0);
filter:grayscale(0);
}
.drinkcard-cc{
cursor:pointer;
padding: 45px;
background-size:contain;
background-repeat:no-repeat;
display:inline-block;
background-position: center;
-webkit-transition: all 100ms ease-in;
-moz-transition: all 100ms ease-in;
transition: all 100ms ease-in;
-webkit-filter: brightness(1.8) grayscale(1) opacity(.8);
-moz-filter: brightness(1.8) grayscale(1) opacity(.8);
filter: brightness(1.8) grayscale(1) opacity(.8);
border-radius: 10px;
.active{
-webkit-filter: brightness(1.1) grayscale(0) opacity(1)!important;
-moz-filter: brightness(1.1) grayscale(0) opacity(1)!important;
filter: brightness(1.1) grayscale(0) opacity(1)!important;
border: 1px solid #007bff;
}
jQuery
$(document).ready(function(){
makeActive();
$("input[type='radio']").on("change",function(){
makeActive();
});
});
function makeActive(){
$("input[type='radio']").each(function(){
if($(this).prop("checked")){
$(this).closest('.option').addClass("active");
}else{
$(this).closest('.option').removeClass("active");
}
});
}
HTML
<div class="form-group col-12 d-block mx-auto">
<h3> Are you Male or Female? </h3>
<div class="cc-selector grid-container">
{{#options}}
<div class="drinkcard-cc option">
<label class="drinkcard-cc icon" id="{{value_id}}" for="{{field_id}}_{{value_id}}"/></label>
<label class="label-text" for="{{field_id}}_{{value_id}}" data-db-localized-content="{{field_id}}_OPTION_{{value_id}}" ></label>
<input
class="image-input drinkcard-cc"
type="radio"
id="{{field_id}}_{{value_id}}"
name="{{name}}"
value="{{value}}" />
</div>
{{/options}}
</div>
</div>
So currently the query adds the .active class to the element with the class #option - I would also like to apply this to #icon
I have tried re-writing this as parent/child and it loses the functionality of the checked radio button and applies the class to every div element (condensed)
var parent ='div'
var child = 'label'
function makeActive(){
$("input[type='radio']").each(function(){
if($(this).prop("checked")){
$(parent).addClass("active");
$(parent +child).addClass("active");
$(parent +child +child).addClass("active");
}else{
$(parent).removeClass("active");
$(parent +child).removeClass("active");
$(parent +child +child).removeClass("active");
I have tried adding:
$("#ICON").addClass("active") or .removeClass
to the relevant parts of the "if/else" parts; However, this does not seem to work.
I am certain the solution is really simple, I am just frazzled from looking at this for so long!
.drinkcard-cc.active .icon { /* beautiful formatting here */ }