How do i make a button swap an image with another image using Jquery?

change image onclick jquery example
change multiple images on click jquery
change image onclick jquery codepen
jquery change image src on load
javascript change image onclick event
jquery toggle image src onclick
change image on button click - javascript
javascript image swap onclick

I am very new to Jquery and am trying to learn it, there seems to be a problem with my code, I want it to change an image on button click but the image isn't changing.

<!DOCTYPE html>
<html>

<head>
  <script src="jquery-3.3.1.min.js">
    $('input').toggle(
      function() {
        $('img').attr('src', '/images/test1.png');
      },
      function() {
        $('img').attr('src', '/images/test2.png');
      }
    );
  </script>
</head>

<body>
  <input value="change image" type="button" />
  <img src="test1.png" />
</body>

</html>
Problem 1

A script element loads a single script.

This can be from code between the start tag and the end tag or a URL specified by the src attribute.

It cannot load both.

If you provide both (as you are doing) the src attribute will be used and the inline script will be ignored.

You need two script elements. One to load the jQuery library and one to load your script which uses that library.

Problem 2

There are no input elements in your document at the time the script runs. See Why does jQuery or a DOM method such as getElementById not find the element?


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  $(function() {
    $('input').toggle(
      function() {
        $('img').attr('src', 'https://placeimg.com/150/150/animals');
      },
      function() {
        $('img').attr('src', 'https://placeimg.com/150/150/people');
      }
    );
  });
</script>
<input value="change image" type="button" />
<img src="https://placeimg.com/150/150/animals" />

How do i make a button swap an image with another image using , Replace an image on click with jQuery, swap the src value of the src attribute value from the click of another element such as a button. jQuery. Each button's CSS class is targetted on click which then targets the ID of the image that changes the src attribute of the image. This uses jQuerys attr () method which can change the value of a given attribute - so also here the alt text value can be changed for each image.

Use this code.

<!DOCTYPE html>
<html>
<head>
 <script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<script type="text/javascript">
$(function(){
$(".img-swap").live('click', function() {
if ($(this).attr("class") == "img-swap") {
  this.src = this.src.replace("_off","_on");
} else {
  this.src = this.src.replace("_on","_off");
}
$(this).toggleClass("on");
});
});
</script>
</head>
<body>
<img src="xxxxxx_off.jpg" class="img-swap" />
</body>
</html>

I think Its work for you.

jQuery replace an image on click via src attribute such as a button, i have 2 image buttons, one toggles a div open and the other toggles it closed. im trying to make it where be default the open button is disabled image, and can't� Changing the image on the click of a button Probably a better use case is changing the image based on some action the user performs. Let’s take a look at changing the image on the press of a button. $ (function () { $ ('#login').on ('click', function () { // Perform some login actions

I recommend to change your html a little bit and would also suggest to use the .click() event this way (with current src check by 'switch' expression) so it changes the image src on button click. HTML:

<button value="change-image">test</button>
<img id='image' src="/images/test1.png" /> 

JS:

$('button').click(
function(){
  switch($('#image').attr('src')){
      case '/images/test1.png':
      $('#image').attr('src', '/images/test2.png');
      break;
      case'/images/test2.png':
      $('#image').attr('src', '/images/test1.png');
      break;
    default:
    //no default action
      break;
  }       
}
);

That worked for me.

jQuery Image Swap Using Click � Design Chemical – jQuery , Answer: Use the jQuery attr() Method. You can use the attr() method to change the image source (i.e. the src attribute of the <img> tag) in jQuery. The following� In the previous tutorial, we looked at how to change an image by clicking on it using an onclick event, a function, and the getElementById() method. However, the image would only change once and when we clicked on the image again it would not change back. In this tutorial, we will be learn how to be able to swap images every time they are clicked.

A Simple jQuery Script to Swap Images On Click, You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover. Let's try� You can display the hidden image using jQuery show/hide function with two buttons. On the click of the show button, the image will be displayed. When you click on the hide button, the image disappears. However, you can also use the jQuery toggle function to perform the show hide effect. For the toggle function, you have to use only a single button which toggles the image. Display Image on Button Click Using jQuery Show/Hide Function

How to Change the Image Source Using jQuery, Approach: Suppose we have an image URL stored in a variable and then use css() method Change background image of a div by clicking on the button See your article appearing on the GeeksforGeeks main page and help other Geeks. Besides the main image, you will want to create a second image to represent the main image. This can either be a graphic, or a thumbnail of the original image as we have done in the example below. You can also use tables to help format the positioning of the main image and thumbnails. If using this technique for a click-to-enlarge gallery, it

How to Change Image on Hover with CSS, The default button in HTML can be changed to an image using CSS. The required The background property can then be set to include a background image and change the image type as required. This effect can be useful in making buttons that react to the mouse. How to change the background image using jQuery ? Here Mudassar Ahmed Khan has explained how to drag and drop images from one DIV to another using jQuery. In order to drag and drop images I have made use of jQuery UI Draggable and Droppable plugins.

Comments
  • Toggle is used to Display or hide the matched elements. api.jquery.com/toggle
  • wrap your js in a document ready and check the image source as you change from test1.png to images/test1.png
  • @Ethanut, You can make it work using the methods on or click then have some logic and and set the src attribute using attr. If its not working post a new question, that way you learn ....
  • "Use this code" — Why? What did you change? Why should it solve the problem? Answers shouldn't be a game of spot the difference.
  • $(".img-swap").live - api.jquery.com/live - that method has been deprecated for years and should not be used.
  • <img src="xxxxxx_off.jpg" class="img-swap" /> — Where has the button gone?
  • hello Quentin, for image swap in jquery we have to use replace instead of toggle
  • in this image, swapping is used so there is no need of a button