Sugarcube 2 how to make random images help

How to show random photos or videos every time we enter different in one passage ?
I wrote this code in the passage, but the photos does not appear

<img id="random-image" src="">
<<script>>
$(document).on(':passagerender', function() {
    // Fotoğraf URL'lerini buraya ekleyin
    var photoURLs = [
        'https://miro.medium.com/v2/resize:fit:728/0*sCItlt4jnF-Q9igZ.jpg',
        'https://www.hepsiburada.com/hayatburada/wp-content/uploads/2023/08/iyi-insan-olmanin-altin-kurallari2.jpg',
        'https://example.com/photo3.jpg',
        // ... diğer fotoğraf URL'leri
    ];

    // Rastgele bir fotoğraf seçme işlemi
    var randomIndex = Math.floor(Math.random() * photoURLs.length);
    var randomPhotoURL = photoURLs[randomIndex];

    // Rastgele seçilen fotoğrafın gösterilmesi
    $('#random-image').attr('src', randomPhotoURL);
});
<</script>>

Hi there!

Unfortunately we can’t see your code well. Could you use the Pre-formatted text (the </> icon) for the whole bit of code?

hi sir every time helping me thanks. I can do maximum this

The problem is that it’s still eating text (and important bit of code). What you want is to do something like this :

image

so it looks like :

THE WHOLE CODE HERE

one minute please

ready

Perfect!

Are you using a nobr tag on the passage? or the Config.passage.nobr in your JavaScript?
(because \\ Comments can break inside of the <<script>> macro)

Otherwise you could use basic Sugarcube:

<<set _image to either("URL1", "URL2", "URL3")>>
<img @src="_image">

The either() function, and the @ Atribute directive

1 Like

No, I started a new story and tried it, that is, I only wrote this code and nothing else. Now I will try the code you gave. Thanks

it worked thank you very much