0

Basically i have a button for which i am trying to make the background change when the user clicks on it.

I know how to do it but i need to use it in a img tag, but it is not working - when i click the button it does not load the image onto it

any help would be appreciated

HTML

<img class="statusYes" src="Buttons/Button-Yes.png" ></img>

jQuery

$(".statusYes").click(function () {
    $(this).css('background-image', 'url(Buttons/Button-No.png)');
});
3
  • 1
    Why does your question and title say "button" when all you have is an image? Commented Nov 18, 2013 at 15:41
  • 1
    When you say "it does not load the image" do you mean it doesn't actually update the background-image property, or that you can't see the background-image because there's an image, the actual img, in the way of the background-image? Commented Nov 18, 2013 at 15:42
  • </img> really? And <input> = <img> ? Commented Nov 18, 2013 at 15:52

4 Answers 4

6

Try something like this:

$(".statusYes").click(function() {  
   $(this).prop("src","Buttons/Button-No.png"); 
});
Sign up to request clarification or add additional context in comments.

Comments

1

the problem is that in your markup you put <img src="..."> and then in the jQuery you try to put a new background to the img element via CSS which is done correctly by the browser, but not shown because it is drawn under the image you put in the src attribute.

you should try:

$(".statusYes").click(function () {
    $(this).attr('src', 'Buttons/Button-No.png');
});

Comments

0
$(".statusYes").click(function(){  
  $(this).css('background', 'url("Buttons/Button-No.png")'); 
});

Comments

0

I'd suggest (given that you appear to be trying to toggle the yes/no options:

$(".status").click(function() {  
    $(this).prop("src",function (i,s){
        return "Buttons/Button-" + (s.indexOf('-No') > -1 ? 'Yes' : 'No') + '.png';
    });
});

This approach, obviously, adjusts the selector, leaving the 'status' to be determined by the current URL of the src property.

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.