V.Cunichin
V.Cunichin

Reputation: 39

change all images on click to its custom attribute

I want to change all pictures on click to their custom attribute for example if you click on 1a.jpg it would change to 1b.jpg.

<!DOCTYPE html>
<html>
<head>
    <style type="text/css"> img {width:100%;} </style>
</head>
<body>
    <img src="1a.jpg"  alt-pic="1b.jpg">
    <img src="2a.jpg"  alt-pic="2b.jpg">
    <img src="3a.jpg" alt-pic="3b.jpg">
<script type="text/javascript"
 src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script type="text/javascript">
$('img').click(function() {
$(this).attr("src", alt-pic)
});
</script>
</body>
</html>

Upvotes: 1

Views: 43

Answers (1)

mplungjan
mplungjan

Reputation: 177965

You mean

$(function() {
 $('img').click(function() {
   var $this = $(this);
   $this.attr("src", $this.attr("alt-pic"));
  });
});

Upvotes: 1

Related Questions