how to change images while onclick in Jquery

Manideepgoud
Posted by Manideepgoud under jQuery category on | Points: 40 | Views : 212
In the below code snippet, am showing how to change the image while onclick in Jquery using Jquery get and set methods.
html-
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="D:/Manideep/practice/css/practice3.css">
<script src="D:/Manideep/practice/js/practice3.js"></script>
</head>
<body>
<div class="container">
<div class="imageseclectsection">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h3 class="maintitle">Changing images</h3>
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-6 col-ms-6 col-xs-6">
<div class="mainimagesection">
<img src="D:/Manideep/practice/Images/a.png" class="mainimage" id="banner"/>
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-6 col-ms-6 col-xs-6">
<div class="imagedescription">
<h5 class="subtitle">Click to here Change</h5>
<ul>
<li>Use Coupon Code WL500 to get Rs 500 off on Whirlpool Washing Machines only on Selected Product ,sold by seller Tata CliQ</li>
<li>Use Coupon Code CLIQLATV500 to get additional Rs. 500 discount on Washing Machines above Rs.25000</li>
<li>Upto 2 Years Extended Warranty Starting Rs. 499/-* if bought together, check details.</li>
</ul>
</div>
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="row myrow">
<div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col-xs-3">
<div class="subimages">
<img src="D:/Manideep/practice/Images/adobe.jpg" class="mainimage"/>
</div>
</div>
<div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col-xs-3">
<div class="subimages">
<img src="D:/Manideep/practice/Images/SystemIntegration 01.jpg" class="mainimage"/>
</div>
</div>
<div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col-xs-3">
<div class="subimages">
<img src="D:/Manideep/practice/Images/homepageV4.jpg" class="mainimage"/>
</div>
</div>
<div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col-xs-3">
<div class="subimages">
<img src="D:/Manideep/practice/Images/Quality.jpg" class="mainimage"/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
</html>

Css-
.maintitle{
text-align: center;
}
.mainimage{
width:100%;
border:1px solid black;

}
.subtitle{
text-align: center;
}
.myrow{
margin-top:5%;
}
.maintitle{
margin-bottom:5%;
}
.mainimagesection{
padding: 10px;
border: 1px solid black;
}


Jquery-
$(document).ready(function(){
$(".mainimage").click(function(){
var selectimages = $(this).attr("src");
$("#banner").attr("src",selectimages);
});
});

Comments or Responses

Login to post response