I would like to create an HTML form for user feedback. If the overall feedback is good, the user should click on a laughing smiley, if the overall feedback is bad, the user
Use jQuery. Keep your checkbox elements hidden and create a list like this:
<ul id="list">
<li><a href="javascript:void(0)" id="link1">Happy face</a></li>
<li><a href="javascript:void(0)" id="link2">Sad face</a></li>
</ul>
<form action="file.php" method="post">
<!-- More code -->
<input type="radio" id="option1" name="radio1" value="happy" style="display:none"/>
<input type="radio" id="option2" name="radio1" value="sad" style="display:none"/>
<!-- More code -->
</form>
<script type="text/javascript">
$("#list li a").click(function() {
$('#list .active').removeClass("active");
var id = this.id;
var newselect = id.replace('link', 'option');
$('#'+newselect).attr('checked', true);
$(this).addClass("active").parent().addClass("active");
return false;
});
</script>
This code would add the checked attribute to your radio inputs in the background and assign class active to your list elements. Do not use inline styles of course, don't forget to include jQuery and everything should run out of the box after you customize it.
Cheers!
Images can be placed in place of radio buttons by using label and span elements.
<div class="customize-radio">
<label>Favourite Smiley</label>
<br>
<label for="hahaha">
<input type="radio" name="smiley" id="hahaha">
<span class="haha-img"></span>
HAHAHA
</label>
<label for="kiss">
<input type="radio" name="smiley" id="kiss">
<span class="kiss-img"></span>
Kiss
</label>
<label for="tongueOut">
<input type="radio" name="smiley" id="tongueOut">
<span class="tongueout-img"></span>
TongueOut
</label>
</div>
Radio button should be hidden,
.customize-radio label > input[type = 'radio'] {
visibility: hidden;
position: absolute;
}
Image can be given in the span tag,
.customize-radio label > input[type = 'radio'] ~ span{
cursor: pointer;
width: 27px;
height: 24px;
display: inline-block;
background-size: 27px 24px;
background-repeat: no-repeat;
}
.haha-img {
background-image: url('hahabefore.png');
}
.kiss-img{
background-image: url('kissbefore.png');
}
.tongueout-img{
background-image: url('tongueoutbefore.png');
}
To change the image on click of radio button, add checked state to the input tag,
.customize-radio label > input[type = 'radio']:checked ~ span.haha-img{
background-image: url('haha.png');
}
.customize-radio label > input[type = 'radio']:checked ~ span.kiss-img{
background-image: url('kiss.png');
}
.customize-radio label > input[type = 'radio']:checked ~ span.tongueout-img{
background-image: url('tongueout.png');
}
If you have any queries, Refer to the following link, As I have taken solution from the below blog, http://frontendsupport.blogspot.com/2018/06/cool-radio-buttons-with-images.html
I have edited one of the previous post. Now, it is way more simple and it works perfectly.
<input style="position: absolute;left:-9999px;" type="radio" name="emotion" id="sad" />
<label for="sad"><img src="red.gif" style="display: inline-block;cursor: pointer;padding: 3px;" alt="I'm sad" /></label>
<input style="position: absolute;left:-9999px;" type="radio" name="emotion" id="happy" />
<label for="happy"><img src="blue.gif" style="display: inline-block;cursor: pointer;padding: 3px;" alt="I'm happy" /></label>
Faced with the same problem I created a simple jQuery plugin http://xypaul.github.io/radioimg.js/
It works using hidden radio buttons and labels containing images as shown below.
<input type="radio" style="display: none;" id="a" />
<label for="a">
<img class="" />
</label>
You can take advantage of CSS3 to do that, by hidding the by-default input radio button with CSS3 rules:
.class-selector input{
margin:0;padding:0;
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
}
And then using labels for images as the following demos:
Here's a pure HTML+CSS solution.
HTML:
<div class="image-radio">
<input type="radio" value="true" checked="checked" name="ice_cream" id="ice_cream_vanilla">
<label for="ice_cream_vanilla">Vanilla</label>
<input type="radio" value="true" name="ice_cream" id="ice_cream_chocolate">
<label for="ice_cream_chocolate">Chocolate</label>
</div>
SCSS:
// use an image instead of the native radio widget
.image-radio {
input[type=radio] {
display: none;
}
input[type=radio] + label {
background: asset-url('icons/choice-unchecked.svg') no-repeat left;
padding-left: 2rem;
}
input[type=radio]:checked + label {
background: asset-url('icons/choice-checked.svg') no-repeat left;
}
}