Code to change an image on button click



Hi, Complete newbie with JS.

Looking for ideas on code to change an image on button click.

Several images available, all with same name plus a number ...
eg: ppImg1, ppImg2, ... ppImg9, ppImg10, etc.

Basic idea is to have a counter set to zero the first time the page is brought up.
Then incremented whenever the "Change Picture" button is clicked.

The site is "under construction", the image changes are to show it is a "working" holding page;
so as to keep visitors' interest. (library of images will be changed fairly often while the site is built).

Any advice / comments / links are welcome.

:smiley: cheers


button.addEventListener('click', function (evt) {
    var count = this.dataset.count || 0
    this.dataset.count = ++count


Hi, Dormilich

Thanks for that. Can just make out how it might work, but haven't worked in JS before.

Not at all sure of "this.dataset." or stringing the "count" variable into the src (filename)

:smiley: ... cheers


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.