Using Images as Radio Buttons - CSS / PHP / Javascript

N

NetwiseHosting

Hi All,

I have trawled the net for well over an hour now, and I have come across a few similar examples, but nothing quite the same as I am looking for. In short I am trying to replace a set of four radio buttons with a set of 4 individual images, with a second set of images for hover and selected - is this possible?

I would have thought I could send the actual radio buttons off screen in CSS and use the label tags to contain the images, and this would work including a hover class but I am not sure about selected. Perhaps Javascript to add a selected class? The only issues is each radio button requires a different image, so a different class would need to be applied for each.

Thanks in advance for your help,

Matthew
 
That's a clever idea to use labels and move the radio buttons off the visible page.

For selected if you have JQuery in the page just create a Click function for each label. Use an If(radio button selected) $("#label_id").css(background: ...) else ...background = ...

You will have to check if the click gets the radio button state before or after it changes and use accordingly.
 
  • Like
Reactions: NetwiseHosting
Upvote 0
N

NetwiseHosting

Hi Guys,

Many thanks for getting back to me. jQuery sounds like a good idea, I will explore this and let you know how I get on.

Regarding leaving form elements unstyled, normally I would agree with you but in this instance we are creating a configuration calculator for one of our services that is highly styled with jQuery UI sliders etc and this is one element we were stuck on.

Thanks for all your input guys,

Matthew
 
Upvote 0

Vennweb

Free Member
Apr 27, 2012
57
6
Northwich
set up images with Jscript to switch on over, click etc. In Jscript write info to a set of hidden input boxes <input type="hidden" id="radio1" .... />
read values from hidden boxes on following page.

Our site uses a lot of image buttons, see jscript there: vennweb.co.uk. This approach will reduce server side calls for data
 
Upvote 0
N

NetwiseHosting

Hi All,

Just a quick follow up message, to say after many hours of coding we have finished our Colocation Calculator which the above questions were related to. You can see the finished product here: http://www.netwisehosting.co.uk/server-colocation/colocation-calculator/index.html which shows the images being used as radio buttons.

If it's of help to anyone, the code behind these radio buttons can be seen on this JS Fiddle: http://jsfiddle.net/HVk7d/18/

The main issue was not displaying the images, this along with the hover function was simple in CSS, but then getting the hover image to 'stick' when selected was the difficulty, especially when each button has a different selected image and therefore the same selected class couldn't be applied to each - if that makes sense! Luckily a little bit of JavaScript came to the rescue here.

Anyway, thanks again for all your assistance,

Matthew
 
  • Like
Reactions: fisicx
Upvote 0

Latest Articles

Join UK Business Forums for free business advice