View Full Version : personalizing radio buttons
Johny159
08-29-2002, 08:58 AM
Is there a way to put an image instead of a radio button, and when the button is clicked to change to another image
glenngv
08-29-2002, 09:28 AM
assuming the images for radio button is radio1.gif for unchecked and radio2.gif for checked:
<img src="radio1.gif" onclick="this.src=(this.src.indexOf('radio1')!=-1)?'radio2.gif':'radio1.gif'">
Johny159
08-29-2002, 10:36 AM
i don't know java script so could you tell me where shoud I put that piece of code
glenngv
08-29-2002, 10:37 AM
since it's just an <img> tag, put it inside the <body> tag
Johny159
08-29-2002, 04:55 PM
OK thanks :thumbsup:
Deanna475
11-22-2003, 10:43 AM
Greetings Glenn. Here's a message that has come back to haunt you! You've helped me out of some pretty tight spots before and I'm hoping you can pull a rabbit out of a hat on this one. *S*
You helped Johny159 perform a nifty image trick to simulate a radio button being selected. Now I need to take it one step further and actually make these images behave as standard radio buttons would. The scenario is easy enough: I've got a CGI script that is going to take care of my mailing list. The radio buttons in question are straight-forward "Subscribe" and "Unsubscribe" buttons.
<input type="radio" name="subscribe" value="subscribe" checked="checked"> Subscribe
<input type="radio" name="subscribe" value="unsubscribe"> Unsubscribe
A working page showing your image trick from the message above in place where the actual radio buttons should be can be seen here: http://www.ldoa.net/index_latest.shtml
If at all possible, can you help me take this personalized radio buttons concept all the way and make it cross-browser compatible?
If Glenn is not around, can anyone recommend a good JS tutorial on this matter or even pick up where Glenn left off? I've searched the web for hours and have only found two tutorials, neither of which I could adapt for my purposes. Any and all assistance is appreciated. ;)
adios
11-22-2003, 10:05 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>untitled</title>
<base href="http://www.ldoa.net/">
<script type="text/javascript" language="javascript">
function fakeRadios(img_name)
{
var i = document.images;
i.subscribe.src = i.subscribe.src.replace(/radio\d/, /radio1/.test(i.subscribe.src) ? 'radio2' : 'radio1');
i.unsubscribe.src = i.unsubscribe.src.replace(/radio\d/, /radio1/.test(i.unsubscribe.src) ? 'radio2' : 'radio1');
document.forms[0].subscription_status.value = img_name;
}
</script>
</head>
<body>
<form>
<table border=0 cellpadding=2 cellspacing=2 width=157 style="margin-top:8px">
<tr><td align=right><img name="subscribe" src="images/radio1.gif" onclick="fakeRadios(this.name)"></td>
<td style="font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:11px; font-weight:bold; color:#000000">
Subscribe</td>
</tr><tr>
<td align=right><img name="unsubscribe" src="images/radio2.gif" onclick="fakeRadios(this.name)"></td>
<td style="font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:11px; font-weight:bold; color:#000000">
Unsubscribe</td></tr>
<tr><td colspan=2 height="10"></td></tr><tr><td>subscription_status: </td><td>
<input type="text" name="subscription_status" value="subscribe">
</td></tr>
</table>
</form>
</body>
</html>
Make that field 'hidden'. :)
Deanna475
11-22-2003, 10:56 PM
Adios!! You are brilliant !! Thank you so much for your attention to my dilemma. I think I am 95% of the way there. The only glitch I'm experiencing now is that when I click the "submit" button, the admin panel for the mailing list script appears (didn't do that to me before -- I used to get the correct "Thank you for subscribing" or "You have been unsubscribed" type of messages). It may be that perhaps the word "subscribe" is used too many times in reference to the form and the CGI script is confused? I dunno. I'm going to have to take a closer look at this. Otherwise, I'm confident once I get that glitch ironed out, the radio buttons will do exactly as they should and it appears to be working in IE, NS, Moz and Opera. At least I'm getting the same result in all 4 browsers, so it's a coding issue and not a browser issue -- thank goodness for that! I'll keep you posted and thanks again SO much for your assistance. It is genuinely appreciated. :)
PS:
HEAD SECTION:
<base href="http://www.ldoa.net/">
<script type="text/javascript" language="javascript">
<!--
function fakeRadios(img_name)
{
var i = document.images;
i.subscribe.src = i.subscribe.src.replace(/radio\d/, /radio1/.test(i.subscribe.src) ? 'radio2' : 'radio1');
i.unsubscribe.src = i.unsubscribe.src.replace(/radio\d/, /radio1/.test(i.unsubscribe.src) ? 'radio2' : 'radio1');
document.forms[0].subscription_status.value = img_name;
}
//-->
</script>
BODY SECTION:
<form name="myform" action="/cgi-bin/subscribe/subscribe.pl" method="post" style="margin-top:8px">
<input type="text" name="email" value="Enter your e-mail address" onfocus="this.value=''" style="font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:11px; color:#000000; background:#D4C784; height:21px; width:154px; border-top:2px solid #826224; border-left:2px solid #826224; border-bottom:2px solid #E6DDB2; border-right:2px solid #E6DDB2; text-align:center"><br>
<table border=0 cellpadding=0 cellspacing=0 width=157 style="margin-top:8px">
<tr><td align=right><img name="subscribe" src="images/radio1.gif" onclick="fakeRadios(this.name)"></td>
<td style="font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:11px; font-weight:bold; color:#000000">
Subscribe</td></td>
<td rowspan=2 align="center" style="background:url(images/join_off.gif); width:52px; height:40px">
<a href="javascript:document.myform.submit()" onfocus="if(this.blur)this.blur()"><img src="images/join_on.gif" name="join" class="imgFader" style="cursor:pointer; cursor:hand" border="0" onMouseOver="JSFX.fadeIn('join')" onMouseOut="JSFX.fadeOut('join')"></a></td></tr>
<tr><td align=right><img name="unsubscribe" src="images/radio2.gif" onclick="fakeRadios(this.name)"></td>
<td style="font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:11px; font-weight:bold; color:#000000"> Unsubscribe</td></tr></table>
<input type="hidden" name="subscription_status" value="subscribe"></form>
This is what I've done in adapting your code to my page. If anything jumps out at you as to why the admin panel is loading on "submit" rather than the appropriate "Thank you for subscribing" or "You have been unsubscribed" or "You have entered an invalid e-mail address" etc. pages, feel free to point it out to me! Sometimes I can sit and stare at the same page, the same code for over an hour and it all begins to look the same. Amazing what a fresh pair of eyes can spot that tired, overworked ones seem to miss. ;)
justame
11-22-2003, 11:16 PM
Originally posted by Deanna475
Amazing what a fresh pair of eyes can spot
dea!!!
just a gigglin'®...sooo glad /mes eyes were just a fresh® n' spotted you...hehehe...hugs n' great to see ya...:O)))
n' whispers...one of these trips when /me goes down to windsor to get the eldest??? /me will just a stop® in for tims™ :O)))
Deanna475
11-22-2003, 11:48 PM
Justame!! So nice to hear from you!! Yes, one day, we will have to go out for that Timmies we keep promising ourselves -- that's a brand of coffee much like "Starbucks" (only better LOL) for the non-Canadians reading this post. :) Just send me an email a day ahead of time the next time you're heading to Windsor.
adios
11-23-2003, 03:28 AM
Deanna -
Did you modify your cgi script to reflect this?
document.forms[0].subscription_status.value = img_name;
Changed it to avoid confusion, client-side...
Hope that's 5% (or so)...
cya, adios
Deanna475
11-23-2003, 10:09 AM
Hi Adios. Uh, no, I didn't modify anything in the CGI script itself. Didn't know I had to!! Don't know what I'm looking for in the CGI script itself that needs to be modified. Gimme a sec and I'll open the script in my editor ........
A moment later ......
Hmmmmm, interesting (looking at the guts of the script -- beyond the variables section -- for the first time)...... would something need to be modified in this section:
###########################
DO NOT EDIT BELOW THIS LINE
##########################
$version = "2.04";
if ($ENV{'QUERY_STRING'}) {
$ENV{'QUERY_STRING'} =~ s/\.\.//g;
$ENV{'QUERY_STRING'} =~ s/\///g;
}
$imagetype = "gif";
$INPUT{'graphic'} = $ENV{'QUERY_STRING'} if $ENV{'QUERY_STRING'} eq 'sub3.gif';
if ($INPUT{'subscribe'} eq "subscribe") { &subscribe; }
elsif ($INPUT{'subscribe'} eq "unsubscribe") {&unsubscribe; }
elsif ($INPUT{'mailing'}) {&mailing; }
elsif ($INPUT{'setpwd'}) {&setpwd; }
elsif ($INPUT{'enterpass'}) {&passcheck; }
elsif ($ENV{'QUERY_STRING'} eq 'sub3.gif') {&display_graphic; }
elsif ($ENV{'QUERY_STRING'} =~ /\@/) {&unsubscribe; }
elsif ($INPUT{'passcheck'}) {&passcheck; }
else {&form; }
exit;
:confused:
adios
11-23-2003, 09:39 PM
Post this to the Perl (http://www.codingforums.com/forumdisplay.php?forumid=5) forum; just preface it by noting that the form element in question (name="subscribe") is now been changed (name="subscription_status").
Or: you could just change all the references yourself and see what happens when you submit. How bad could it be?
if ($INPUT{'subscription_status'} eq "subscribe") { &subscribe; }
elsif ($INPUT{'subscription_status'} eq "unsubscribe") {&unsubscribe; }
donmarvin
10-12-2006, 07:40 PM
This was exactly what I was looking for -- more than 4 years after it was posted! :thumbsup:
assuming the images for radio button is radio1.gif for unchecked and radio2.gif for checked:
<img src="radio1.gif" onclick="this.src=(this.src.indexOf('radio1')!=-1)?'radio2.gif':'radio1.gif'">
donmarvin
10-12-2006, 08:43 PM
Here are some cool Yes/No check boxes to use with the code above. I tried to upload the psd files so you can change the color of the checks, but they were too big. Email me if anyone needs them!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.