Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I'm making a small program that displays the images from a website. How it would be possible to use the onclick event in such a way that each click will increase the number of url eg / images/P100_01.gif -> / images/P101_01.gif and print the image in HTML?
Posted

1 solution

Learn and adapt from the following example:
<!DOCTYPE html>
<html>
<body>
<p>Click to get next image</p>
<image src="/images/P100_01.gif" id="img">
<button type="button" onclick="getNextImage()">Get Image</button>
<script>
function getNextImage() {
// get the old image source
var oldImageSource = document.getElementById("img").src;
alert(oldImageSource);
// extract the '100'
var str = oldImageSource.slice(9,12);
// convert '100' to number
var oldNum = Number(str);
alert(oldNum);
// increase 100 by 1
var newNum = oldNum + 1;
alert(newNum);
// replace the old number in the old image source by the new one
var newImageSource = oldImageSource.replace(oldNum, newNum); 
alert(newImageSource);
}
</script>
</body>
</html> 

Read more on JavaScript String Object[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900