3

I was wondering on how I could go about recreating a button pressed effect using JQuery.

My ideas where to use a event for mouse click to change image to pressed image and than after mouse releases, return back to original image. What are you thoughts or ideas on ways I could accomplish this?

2

3 Answers 3

3

That should work. The code would be:

$("#button").mousedown(function(){
$("#img").attr("src", "img2.jpg");
})
$("#button").mouseup(function(){
$("#img").attr("src", "img1.jpg");
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad to help. I would appreciate a check, both to stop other people from looking at this and help my abysmal reputation.
1

Have the image listen to the click event and change the source of the image.

$('img').on('click', function() {
    var $this = $(this);

    $this.attr('src', function(i, src) {
        return src === 'something' ? 'somethingelse' : 'something' ;
    });

});

2 Comments

Thanks :) I like @newbie's solution better though.
Glad to have helped :)
0

What do you want exactly ? something like this is the way ...

$('btn-img').click(function(){
   var img =  $(this).children('img'); // return object of inner img
   // your event on current img with click
}).mouseleave(function(){
   // other event on mouseleave
});

Html :

<a class="btn-img">
    <img src="yourImage.jpg" />
</a>

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.