I have created one html page that shows images & navigates to other images based on right and left arrow key press.
There is an anchor tag with download attribute. The path of images which is displayed on screen currently is set to href of this tag through jquery.
Clicking this link downloads the image. I need that on pressing of down arrow, image should be downloaded. (in short this anchor tag's click event should be triggered.)
I tried jquery trigger function but not working. Below is my code.
Thanks in advance.
<!doctype HTML>
<html>
<head>
<title>My Page</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style type="text/css">
div.links {
top: 95%;
left: 45%;
position: fixed;
}
a.dLink, a.nav {
text-decoration: none;
}
label {
background: #330033;
padding: 2px;
color: #CC9933;
cursor: pointer;
}
</style>
</head>
<body>
<input type="hidden" id="iNames"
value="IMG1.jpg,IMG2.jpg,IMG3.jpg">
<img class="pic" />
<div class="links">
<label class="prev">Prev</label>
<a class="dLink" href="" download>
<label>Download</label>
</a>
<label class="next">Next</label>
</div>
<script type="text/javascript">
$(document).ready(
function() {
iNames = ($("#iNames").val()).split(",");
total = iNames.length;
i = 0;
$("img.pic").attr("src",iNames[i]);
$("a.dLink").attr("href", $("img.pic").attr("src"));
$(document).keydown(function(e) {
switch (e.which) {
case 37: // left
prev();
break;
case 39: // right
next();
break;
case 40: // down
$("a#dLink").trigger("click"); //this is not working
break;
default:
return;
}
});
function next() {
i = i == total - 1 ? 0 : i + 1;
setImage();
}
function prev() {
i = i == 0 ? total - 1 : i - 1;
setImage();
}
function setImage() {
$("img.pic").attr("src",iNames[i]);
$("a.dLink").attr("href", $("img.pic").attr("src"));
}
$("label.next").click(function() {
next();
});
$("label.prev").click(function() {
prev();
});
});
</script>
</body>
</html>