Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap button for image (Jquery)

I have a button and when it's clicked, I want to replace the button with an image. How can I do this in JQuery? Is it possible to replace the background of the image as well? The button itself is within a large div, and I don't want to add another div around the button because it messes up a previous layout.

like image 357
Skoder Avatar asked Feb 28 '23 07:02

Skoder


1 Answers

If you want to replace the button element:

$('the-button').bind('click', function () {
    $(this).replaceWith('<img src="/wherever.jpg"/>');
});

If you want to change the background image of the button:

$('the-button').bind('click', function () {
    $(this).css('backgroundImage', 'url(\'/wherever.jpg\')');
});

If you want to change the background image of the image (:S):

$('the-button').bind('click', function () {
    $(this).replaceWith('<img src="/wherever.jpg" style="background-image:url(\'/somewhere-else.jpg\');"/>');
});
like image 130
Matt Avatar answered Mar 05 '23 16:03

Matt