1

I am trying to add an if else statement to the below

$('#togglediv4').click(function(e){
     e.preventDefault();
    $('#link4').attr('src','images/contact_link_clicked.png');
});

I want the image to change back to the original one if there is no click event otherwise all links once clicked will remain stuck on the new image.

Therefore if image is not clicked I want to show the original image.

$('#link4').attr('src','images/contact_link_original.png');
5
  • I lost you half way there...so whatis it exactly that u want??? Commented Dec 28, 2012 at 9:59
  • is this jquery? if so, tag it as such Commented Dec 28, 2012 at 9:59
  • u want to add if else..then add it why ask??? Commented Dec 28, 2012 at 10:02
  • I tried to add if else but cannot get the correct format right Commented Dec 28, 2012 at 10:04
  • Do all your images have a common class? Commented Dec 28, 2012 at 10:13

4 Answers 4

2

Use attr callback function and ternary operator.

$('#togglediv4').click(function(e){
     e.preventDefault();
     $('#link4').attr('src',function(i, src){
          return src === 'images/contact_link_original.png'
          ? 'images/contact_link_clicked.png'
          : 'images/contact_link_original.png'
     });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You should just set the src attribute of the original <img> tag to: mages/contact_link_original.png

Then add the click event as above.

By default when your div hasnt been clicked it will contain the original image.

EDIT: based on your comment I would do the following:

  • Add a class to all the images
  • When any image is clicked change the src attr back to the original on ALL images with that class. (You can use $('.imgClass'))
  • Then update the image that was clicked with the new image.

3 Comments

but once clicked I do want the image to turn back to the original as soon as another image is clicked
this $('.imgclass').attr('src','images/newhome_link.png'); specifies all images within the class, can I specify each image src separately within the class ?
for each click I turned back the other images links to their original images. I specified the src for each image in all functions.
0

I may not be understanding your question correctly, but it looks to me like you need to use the mousedown and mouseup events instead of click:

$('#togglediv4').mousedown(function(e){
    e.preventDefault();
    $('#link4').attr('src','images/contact_link_clicked.png');
}).mouseup(function(e){
    e.preventDefault();
    $('#link4').attr('src','images/contact_link_original.png');
});

2 Comments

unfortunately this works only if the mouse is left clicked down, else it will turn back to original. I want to keep the image changed until another image is clicked
@user1347219 Ah, I wasn't understanding your question correctly then. In that case I'd look at undefined's answer.
0

I think you are looking for something like this:

$('#togglediv4').click(function(e){
    e.preventDefault();
    if ($('#link4').attr('src') == 'images/contact_link_original.png') {
        $('#link4').attr('src','images/contact_link_clicked.png');
    }       
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.