not sure what you mean by "generate a js script to do something", however adding an event listener to a button is easy: ``<i> </i><button id="my-button">Click!</button><i> </i>`</CODE> js <CODE>`<i> </i>document.getElementById('my-button').addEventListener('click', event => { // button was clicked, do anything you like here });<i> </i>``
//insert the URL of images in array randomImage[0] = "https://images.pexels.com/photos/858115/pexels-photo-858115.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"; randomImage[1] = "http://www.petsworld.in/blog/wp-content/uploads/2014/09/running-cute-puppies.jpg"; randomImage[2] = "https://images.pexels.com/photos/142497/pexels-photo-142497.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"; randomImage[3] = "https://images.unsplash.com/photo-1543877087-ebf71fde2be1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"; randomImage[4] = "https://wi.wallpapertip.com/wsimgs/156-1565522_puppies-desktop-wallpaper-desktop-background-puppies.jpg"; randomImage[5] = "https://images.unsplash.com/photo-1501265976582-c1e1b0bbaf63?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60";
//generate a number and provide to the image to generate randomly var number = Math.floor(Math.random() * randomImage.length);
//return the images generated by a random number // There is no element document.canvas in your document: // document.canvas.src = '<img src="' + randomImage[number] + '" />'; // assign url of random image to src attribute of img tag inside the figure tag: document.querySelector('figure.item4 img').src = randomImage[number]; }
let timer = null;
`` ---------------------------------------------
On the page i have a button called generate pics and when i press i want to active the above js-file. How to do?
@SempervivumMar 09.2022 — #Simply call the function by the event listener: ``<i> </i>document.getElementById('my-button').addEventListener('click', event => { // button was clicked, do anything you like here getRandomImage(); });<i> </i>``
@SempervivumMar 09.2022 — #Place the javascript at the bottom of the body, right before the closing `</body>`. The reason is that the button has to be defined before the listener is added.