imageTick for jQuery (Pretty Checkboxes) June 8, 2008 39 Comments
It’s Sunday afternoon and it is probably the hottest day Ottawa has been hit with so far. So I went out to The Bay, bought an air conditioner, sat on my computer and hacked away. I decided to make a small jQuery plugin that provides an unobtrusive approach to using images as checkboxes and radio buttons. I had seen a few out there that just take care of the checkboxes and that don’t work at all with labels. I figured that I can whip something up that might help a lot of people out. The script figures out whether it’s a checkbox or a radio button – you just have to provide some styling info.
If the user has Javascript turned off a 100 gig virus will be uploaded to their network, their cars gas tank will fill up with salt and the sky will fall on them… Ok not really, it will just show the regular inputs.
Usage
The chunk of javascript below goes in the head of your document.
<script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/imagetick.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input[name='favlang']").imageTick({ // the selector can be a class as well, target your radios or checkboxes tick_image_path: "images/radio.gif", // the image you want to use as a selected state of the radio/checkbox no_tick_image_path: "images/no_radio.gif", // image you want to use as a non selected state image_tick_class: "radios" // the class you want to apply to all images that are dynamically created }); }); </script>
HTML
Here’s some HTML that goes with the example
<h2>Radio Buttons</h2> <h4>Pick a language</h4> <ul> <li> <input type="radio" value="php" id="php" name="favlang" /> <label for="php">PHP</label> </li> <li> <input type="radio" id="javascript" value="javascript" name="favlang" checked="checked" /> <label for="javascript">Javascript</label> </li> <li> <input type="radio" id="html" value="html" name="favlang" /> <label for="html">HTML</label> </li> <li> <input type="radio" id="sql" value="sql" name="favlang" /> <label for="sql">SQL</label> </li> <li> <input type="radio" id="japanese" value="japanese" name="favlang" /> <label for="japanese">Japanese</label> </li> </ul>


http://dev.jquery.com/~gilles/cssHover/
image_tick_class: "ticks_" + Math.floor(Math.random() * 999999),
or some other method to create a unique identifier for the class. Otherwise, if you mix and match radios and checkboxes on one page, checkboxes turn into no_radios on radiobutton.click(). Otherwise, solid little plugin, thanks!
I'm trying to get the example working in a setup with Django on a generated form. The problem, as it seems, and I hope you can confirm this so at least I know what I'm doing :)
The input tags have an id attrib that ends in _0 _1 _2 etc.
I'm trying to render img tags with the source image names having a suffix like so:
I can't get the label text to fill out the image name that is returned. Otherwise the (radio) type selection and form submission work perfectly.
Can the funky id name be the cause of this?
Thanx a lot!
Regards,
Gerard.
I'm trying to render img tags with the source image names having a suffix like so:
http://www.gerardjp.com/dl/code/imagetick.txt
(sorry for the spam :-/ )
http://tinyurl.com/33j9gt6
Is this something you can modify in a django form? I've heard django forms can be tricky sometimes. If you cannot change the "for" attribute on the label to match the "id" attribute of the input, let me know and I will build in a work-around.
Thanx for the response, Oh, and don't worry about the delay, I was probably to anxious to solve my issue :)
I checked in regards to django forms and it does, by default, render a proper 'for=' related to the input id. My own renderer for the radio widget didn't use the proper one.
Using a radio selection directly related to key words stored in the DB and also suffixing image names might be odd (mis) usage. However, I still needed the 'value' as proposed in the aformentioned stackoverflow Q.
I'm admitting I'm not a jquery diehard, and not completely overseeing the use but might this be an idea. Using a 'tick_image_radio_prefix_path' so users can have different images per item in a radio input list. should anyone ever need that :-/
(This in supplement on the stackoverflow issue (http://tinyurl.com/3xrf3tx) about custom images.)
Just my two cents :)
I'm anyway happy that my response resulted in a improvement on you very charming script!
Thanx again.
Regards,
Gerard.
I wanted to have a pointer as cursor when I used imagetick. So I did this:
$("#cbxStayLoggedIn").css('cursor', 'pointer'); /* does not work*/
$("#cbxStayLoggedIn").prev().css('cursor', 'pointer'); /* does not work*/
$("#cbxStayLoggedIn").imageTick({ /* this works :) but no hand cursor... */
tick_image_path: '/images/checkbox-on.gif',
no_tick_image_path: '/images/checkbox-off.gif'
});
I seem to be having a problem with it though, if I attach a change event to some radio buttons that've been tweaked with imageTick, the events don't fire, for example:
$("input[name='radioStation']").change(function() { alert('changed'); });As a workaround I've tried using a click event instead, but often this isn't being fired when a radio button is first selected and isn't really what I want to do anyway.
I'm testing this in Fx 3.6.13, Chrome 8, Safari 5 and IE8 on Win XP, and the page is running jQuery 1.4.4 if that's any help. I can't see how or why imageTick and change events don't play nicely together, very strange issue!
I want 3 images as radio buttons in my form.
So I have 3 input elements with different IDs and ImageTick sets the images for them.
But since I click on one of the images, ImageTick sets the "pressed" image on all 3 radio buttons. Did I do anything wrong?
my code:
$(document).ready(function(){ $("#mode_country").imageTick({ tick_image_path: "./images/radio_countries.png", no_tick_image_path: "./images/no_radio_countries.png", image_tick_class: "radios1" }); $("#mode_continent").imageTick({ tick_image_path: "./images/radio_continent.png", no_tick_image_path: "./images/no_radio_continent.png", image_tick_class: "radios1" }); $("#mode_world").imageTick({ tick_image_path: "./images/radio_world.png", no_tick_image_path: "./images/no_radio_world.png", image_tick_class: "radios1" }); });and a radiobutton looks like:
Is there no way to get this to work?
I'm not that familiar with scripting, maybe there is another way to do this?
Thank you!
Robert
Serviu perfeitamente o código.
Estava procurando isso há tempos.
Thanks!
If you make each unique for that set of radio buttons, things seem to work just fine.
1)how can i reset radio/checkbox
2)how can i check/uncheck
can anybody help me?
1)how can i reset radio/checkbox
2)how can i check/uncheck
http://jsfiddle.net/jboesch26/ALYjD/1/
how can we distinguish between groups of radio buttons.
i have groups which have GroupName property:
how can i differentiate in : $("input[type='radio']").imageTick({,
i tryed this line but it is not working:
$("input[name= " + $("#") + "Question1']").imageTick({
thank you :)
http://stackoverflow.com/questions/3114166/replacing-radio-buttons-with-different-images
basically...one radio button set with a different image for each radio button.
Thanks!
Great job.
A question: How to center the image that replaces the radio button?
First - thank you Jordan for the great script. Echoing Inbal above, I am struggling with how to designate different radio groups on the same page. Currently, I can only have a single radio group on a page - otherwise, the 'selected image' is replaced for all groups with each selection. In other words, only one radio button can display as selected on any page at a given time. Has anyone found a solution for this?
Thanks!
jj
First of all - thank you for this script.
However I have some problems with it.
When I click a radio button, it changes the image to what I have set in tick_image_path, yes.
When I click another radio button in the same group, the first does not go back to the 'unchecked' image I have defined in no_tick_image_path. The path is correct however since the right image is shown when loading the page, as in nothing is checked.
Can you help me?
$("input[id='freedom']").imageTick({ // the selector can be a class as well, target your radios or checkboxes
tick_image_path: "images/button-free.a.png", // the image you want to use as a selected state of the radio/checkbox
no_tick_image_path: "images/button-free.png", // image you want to use as a non selected state
});
There's the code for the radio button and the imagetick jquery stuff.