add an image to a html type input check box or radio
Solution 1
Take a look at this Ryan Fait - Custom checkboxes and radio buttons ... Hope it's what you're looking for ;)
Solution 2
I have found and worked an answer. with help from these forums http://forums.digitalpoint.com/showthread.php?t=665980
all code is below: you can copy to a single file and add your own images and it will work.
<html>
<script type="text/javascript">
//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = '52 0 ROff.png';
var imgTrue = '52 0 ROn.png';
//replace the checkbox with an image and setup events to handle it
function replaceChecks() {
//get all the input fields on the page
inputs = document.getElementsByTagName('input');
//cycle trough the input fields
for(var i=0; i<inputs.length; i++) {
//check if the input is a checkbox
if(inputs[i].getAttribute('type') == 'checkbox') {
//create a new image
var img = document.createElement('img');
//check if the checkbox is checked
if(inputs[i].checked) {
img.src = imgTrue;
} else {
img.src = imgFalse;
}
//set image ID and onclick action
img.id = 'checkImage'+i;
//set image
img.onclick = new Function('checkClick('+i+')');
//place image in front of the checkbox
inputs[i].parentNode.insertBefore(img, inputs[i]);
//hide the checkbox
inputs[i].style.display='none';
}
}
}
//change the checkbox status and the replacement image
function checkClick(i) {
if(inputs[i].checked) {
inputs[i].checked = '';
document.getElementById('checkImage'+i).src=imgFalse;
} else {
inputs[i].checked = 'checked';
document.getElementById('checkImage'+i).src=imgTrue;
}
}
</script>
</html>
<body>
<input type="checkbox" />
<script type="text/javascript">replaceChecks();</script>
</body>
Solution 3
In checkbox if you want to change images or/and color then the optimized way is http://jsfiddle.net/nsoni/4cHSB/5/ here you can click either on "item name" or on "checkbox" and get the effect.
HTML:
<div class="had">
<div class="ch">
<input type="checkbox" id="sho1" name="chk1">
<div class="so"></div>
<label for="sho1">Select one</label>
</div>
<div class="ch">
<input type="checkbox" id="sho2" name="chk2">
<div class="so"></div>
<label for="sho2">Select two</label>
</div>
</div>
CSS:
input {
display: none;
}
input [type=checkbox]:checked+.so{
background-image:url('http://goo.gl/3tza1X');
background-repeat:no-repeat;background-color:green;
border-radius:50%;background-position: -2px -1px;
}
input [type=checkbox]:checked+.so+label{
color: red;
}
.so {
margin: 2px; float: left;height: 30px;width: 30px;
border: 3px solid #D8D8D8;background-color: #4679BD;
border-radius: 5px;
}
.show {
margin-left: 30px;
}
.had {
margin: 50px auto;width: 50%;border: 1px solid black;
height: 30px;padding:10px;
}
.ch {
float: left;margin-left:20px;width:40%
}
label {
height: 30px;width: 100%;position:relative;
display:block;margin-top: 5px;
}
f1wade
Updated on April 16, 2020Comments
-
f1wade about 4 years
I'm trying to specify an image to be used for my unchecked and checked values for html input type checkbox and radio.
I have got this:
background: url("image.png") no-repeat;
but it doesnt seem to work on radio and checkbox only button.
Does anyone know something that will work?