how do you toggle two images based on a button click


I am just trying to toggle two images based on the onclick of a button.

The trick is that whatever image that is currently showing I need it to display the other one on the onclick.

I have some collapsible div's on a  page with two images ("more.jpg" and "less.jpg")

You can select one div at a time to collapse or expand or you can push a button and have them all expand.

The problem is that if a user hits one div to expand it then hits the "all expand" button one of the images will be still set to "more.jpg" with the div expanded (should be less.jpg"

Is there any way to just toggle two images back and forth independently of what the other images are doing?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Have you looked at the JQuery .toggle() function
<div class="imagebox">
   <img src="more.jpg" />
<div class="imagebox" style="display:none">
   <img src="less.jpg" />
<button id="switch">click</button>
<script src=""></script>
<script type="text/javascript">
$(function() {
   $('#switch').click(function() {

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. can you try this code please

Try this:

$(document).ready(function() {
  $("input[name=jpgSel]").change(function () {
    if (this.value == "0") {
        src: 'one.jpg',
        alt: 'one dot jpg'
    } else {
        src: 'two.jpg',
        alt: 'two dot jpg'

Open in new window

smfmetro10Author Commented:
Outstanding! Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.