Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <button data-type="randomize">Go</button>

<div class="card | card--unselected" data-card-id="1">
    <h3>Selection 1</h3>
</div>

<div class="card | card--unselected" data-card-id="2">
    <h3>Selection 2</h3>
</div>

<div class="card | card--unselected" data-card-id="3000">
    <h3>Selection 3000</h3>
</div>

<div class="card | card--unselected" data-card-id="50">
    <h3>Selection 50</h3>
</div>
              
            
!

CSS

              
                .card {
  background-color: blue;
}

.card--unselected {
  background-color: red;
}
              
            
!

JS

              
                // highlight different 'cards' randomly upon clicking the 'go' button
// never displays the same card twice in a row (well, unless when resetting)
// once all cards have been displayed once, then it resets to display them again in another random order

const random = () => {
    const button = document.querySelector('button[data-type="randomize"]')
    const cards = document.querySelectorAll('.card')

    // array that contains our card IDs
    const cardsCollection = Array.from(cards, (card) => card.getAttribute('data-card-id'));

    // array that keeps track of cards that have already been selected
    const cardsSelected = []

    button.addEventListener('click', () => {
        // generate a random number (randomNumber) that is less than the amount of items in the cardsCollection array
        // check if the random number exists as an index in the cardsSelected array
        // if it does, then run again
        // if it does not, then push that random number into the cardsSelected array
        let randomSelection;
        do {
            const randomNumber = Math.floor(Math.random() * cardsCollection.length)
            randomSelection = cardsCollection[randomNumber]
        } while (cardsSelected.includes(randomSelection))
        cardsSelected.push(randomSelection)

        // if the cardsSelected array contains all of the possible random numbers
        // then reset it to 0 so that it can start over
        if (cardsSelected.length >= cardsCollection.length) {
            cardsSelected.length = 0
        }

        cards.forEach((card) => {
            // hide all cards by adding the appropriate CSS class
            card.classList.add('card--unselected')

            // display the randomly selected card by adding the appropriate CSS class
            if (card.dataset.cardId === randomSelection) {
                card.classList.remove('card--unselected')
            }
        })
    })
}
random()
              
            
!
999px

Console