Why does my JavaScript work in the browser console but does not work in my shortcode?

Solved by wrapping everything in document.addEventListener(“DOMContentLoaded”, function() {}

Realized it was trying to fire the script off

document.addEventListener("DOMContentLoaded", function () {
        var getSiblings = function (elem) {

            // Setup siblings array and get the parent
            var siblings = [];
            var sibling = elem.parentNode.parentNode;

            // Loop through each sibling and push to the array
            while (sibling) {
                if (sibling.nodeType === 1) {
                    siblings.push(sibling);
                }
                sibling = sibling.nextSibling;
            }
            return siblings;
        };

        var elem = document.getElementById("read-more-btn");
        var siblings = getSiblings(elem);

        siblings.forEach(function(value, index) {
            if (index >= 1) {
                value.style.display ="none";
            }
        });

        readMore.addEventListener("click", function(){
            console.log("click works");
            siblings.forEach(function(value, index) {
                if (index > 1) {
                    readMore.style.display = "none";
                    value.style.display = "block";
                }
                
            });
        });
    });

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)