2

I have five buttons when I click on one button one image comes in a div tag again when I click on second button another image should replace that image with a new one and so on. what code should I write to achieve it.

please help me do that. may it be javascript or jquery. I think something to do with onclick event.

Steve Greatrex
  • 15,789
  • 5
  • 59
  • 73
Php developer
  • 446
  • 4
  • 18

3 Answers3

2
         <div class="imageSelector" />


  $(".btnOne").on("click", function(){

  $(".imageSelector").html("put here the html code of the image")

  })


  $(".btnTwo").....
luisZavaleta
  • 1,160
  • 11
  • 21
2

Simple as below,

<script type="text/javascript">
    $(function(){

        $('button').on('click',function(e){
            e.preventDefault();

            var imgSRC = $(this).data('src');

            $('#image').html('<img src="'+imgSRC+'" alt="" />');
        });

    });
</script>

<button data-src="image1.jpg">One</button>
<button data-src="image1.jpg">Second</button>
<button data-src="image1.jpg">Third</button>
<button data-src="image1.jpg">Fourth</button>
<button data-src="image1.jpg">Fifth</button>

<div id="image"></div>

Working DEMO

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

OldImage value by default set the first image url and in function NewImage url of new image will be occur which may be id of button..

var oldImage="img.png";
function ImageChange(NewImage)
{
    $("#divId").replace(oldImage,"newImage");
    oldImage=newImage;
}
S. S. Rawat
  • 5,943
  • 4
  • 43
  • 59