In the checkout page I want to add separate images instead of each radio button. I found how to do this here.
The problem here is that I cannot add a separate ID or class to the label element and it seems that is required to replace the radio buttons with an actual image. Below what I got so far. I cannot target the labels specifically as shown on the GitHub thread.
Does anyone have an idea how to add a class using PHP or JavaScript maybe? Or any other way for that matter.
Thanks in advance!
Here the HTML:
<span class="woocommerce-input-wrapper">
<input type="radio" class="input-radio " value="Banana" name="radioButtonForm" id="radioButton1">
<label for="radioButton1" class="radio ">Banana</label>
<input type="radio" class="input-radio " value="Apple" name="radioButtonForm" id="radioButton2">
<label for="radioButton2" class="radio ">Apple</label>
<input type="radio" class="input-radio " value="Pear" name="radioButtonForm" id="radioButton3">
<label for="radioButton3" class="radio ">Pear</label>
<input type="radio" class="input-radio " value="Tomato" name="radioButtonForm" id="radioButton4">
<label for="radioButton4" class="radio ">Tomato</label>
</span>
And here the CSS:
.woocommerce-input-wrapper input{
margin:0;padding:0;
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
}
#radioButton1 .radio{
background-image:url(http://i.imgur.com/SJbRQF7.png);
}
#radioButton2 .radio{
background-image:url(http://i.imgur.com/lXzJ1eB.png);
}
#radioButton3 .radio{
background-image:url(http://i.imgur.com/SJbRQF7.png);
}
#radioButton4 .radio{
background-image:url(http://i.imgur.com/lXzJ1eB.png);
}
2
Answers
We can use
preg_replace
to target our labels tag and add our classes.If I understood your problem correctly, you don’t needd to necessarily add custom
id
s orclass
es to your markup. Switching your CSS selectors to use the+
-sibling selector should suffice. I made a JSFiddle to demonstrate this kind of solution:https://jsfiddle.net/6rsf79xz/