I am in pursuit of implementing images as checkboxes. For now I am trying out a sample. The code below contains a simple image with a submit button next to it. On clicking the submit button I want the image to develop a border around it and on clicking the submit button, I want the checkbox value to be passed.
<html>
<script>
$(document).ready(function() {
$('#blr').click(
function(){
$('#blr').css('border', 'solid 2px red');
$('#imgCheck').attr('checked', 'checked');
},
function(){
$('#blr').css('border', 'none');
$('#imgCheck').removeAttr('checked');
}
);
});
</script>
<form id="form1">
<img src="icons/blr.jpg" title="blr" id="blr" />
<input type="checkbox" id="imgCheck" name="imgCheck" value="barney" style="visibility: hidden;" />
<input type="submit" value="Submit" />
</form>
</html>
I am relatively new to Jquery and I am not able to figure out where am I going wrong. Any help will be greatly appreciated.
Thanks in advance :)
Actually using image as checkbox can be done with HTML & CSS ONLY!
The trick is to style a
<label>
element (make it an image) and add it afor="checkboxid"
parameter - then just make a<checkbox>
with a properid="checkboxid"
and hide it. When you click on label => the checkbox gets (un)checked. Also the usage of:checked
and+
selector is good if you want to change label image on checked / unchecked.HTML
CSS
Fiddle - edited/simplified: http://jsfiddle.net/bdTX2/
Example took from: http://www.csscheckbox.com/
The click Function doesn't work like what your thinking.... the way you are looking for is for Toggle Try this ..I think This Will help ..Cheers
Another Solution for CSS-Only
Use
-webkit-apperance: none
to 'hide' the original checkbox, and then style it as you want.To style, when checkbox is checked, simple use this pseudo code:
input[type="checkbox"]:checked
HTML
CSS
Demo Fiddle
Here is the solution of your question. I hope this will help you.
CSS
HTML Code
jQuery Code
Working Example : Demo
Click doesn't work like that, you can't toggle two functions. You must use an if statement like this
DEMO
You could shorten the code even more like this
DEMO