June 29, 2010

Using jQuery to Toggle Images with Radio Buttons




Here’s a simple example to toggle Images using Radio Buttons:

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>Toggle Images using RadioButtons</title>
<
script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js">
</
script>
<
script type="text/javascript">
$(document).ready(function() {
$("input:radio[name=rdImg]").change(function() {
if (this.value == "0") {
$("#imgOne").attr(
'src', 'http://theimagepathcomeshere.jpg'
);
}
else {
$("#imgOne").attr(
'src', 'http://thesecondimagepathcomeshere.jpg'
);
}
});
});
</script>
</
head>
<
body>
<
label><input name="rdImg" type="radio" value="0" />Image 1</label><br />
<
label><input name="rdImg" type="radio" value="1" />Image 2</label><br />
<
img id="imgOne" />
</
body>
</
html>

Similarly you can add multiple radio buttons and toggle multiple images with it.

Just use the value of each radiobutton to toggle the right image.

See a Live Demo



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Using jQuery to Toggle Images with Radio Buttons"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions