0

I have a file written in vanilla js. When I import jquery using: import $ as "jquery" all code stops working. Of course, I put this at the beginning of the code, the import line. when I click on the item with the class name ".search" nothing happens. When I delete: import $ from "jquery" everything is back to normal. The search overlay opens.

import $ from "jquery"

class Search {
    constructor() {
        this.resultsDiv = document.querySelector("#results")
        this.openButtons = document.querySelectorAll(".search");
        this.closeButton = document.querySelector(".search-close");
        this.searchOverlay = document.querySelector(".search-overlay");
        this.searchField = document.querySelector("#search-input");
        this.previousValue;
        this.typingTimer;
        this.isOverlayOpen = false;
        this.isSpinnerVisible = false;
        this.events();
    }

    // 2. events
    events() {
        this.openButtons.forEach((button) =>
            button.addEventListener("click", this.openOverlay.bind(this))
        );
        this.closeButton.addEventListener("click", this.closeOverlay.bind(this));
        document.addEventListener("keydown", this.keyPressDispatcher.bind(this));
        this.searchField.addEventListener("keyup", this.typingLogic.bind(this));
    }

    // 3. methods (function, actions...)
    typingLogic() {
        if (this.searchField.value != this.previousValue) {
            clearTimeout(this.typingTimer);

            if (this.searchField.value) {
                if (!this.isSpinnerVisible) {
                    this.resultsDiv.innerHTML = '<div class="spinner-loader"></div>';
                    this.isSpinnerVisible = true;
                }
                this.typingTimer = setTimeout(this.getReults.bind(this), 2000);
            } else {
                this.resultsDiv.innerHTML = '';
                this.isSpinnerVisible = false;
            }
        }
        this.previousValue = this.searchField.value;
    }

    getResults() {
        $.getJSON("/wp-json/wp/v2/posts?search=" + this.searchField.val(), function (posts) {
            alert(posts[0].title.rendered)
        })
    }

    keyPressDispatcher(e) {
        if (e.keyCode == 83 && !this.isOverlayOpen && !document.querySelectorAll("input, textarea").activeElement) {
            this.openOverlay();
        }

        if (e.keyCode == 27 && this.isOverlayOpen) {
            this.closeOverlay();
        }
    }

    openOverlay() {
        this.searchOverlay.classList.add("search-overlay--active");
        document.querySelector("body").classList.add("body-no-scroll");
        this.isOverlayOpen = true;
    }

    closeOverlay() {
        this.searchOverlay.classList.remove("search-overlay--active");
        document.querySelector("body").classList.remove("body-no-scroll");
        this.isOverlayOpen = false;
    }
}

const search = new Search();

I don't know what is causing the problem.

treckstar
  • 1,956
  • 5
  • 21
  • 26
  • 1
    Does this answer your question? [How to import jquery using ES6 syntax?](https://stackoverflow.com/questions/34338411/how-to-import-jquery-using-es6-syntax) – GrafiCode Apr 27 '22 at 11:36
  • 2
    In general, you should post what kind of error you see to help up help you. I am assuming something happens in the browser console. – AsTeR Apr 27 '22 at 11:38
  • 1
    `using: import $ as "jquery"` would be a syntax error. Please show what you're **actually** doing, and quote the error message you get in the devtools console (using copy and paste). – T.J. Crowder Apr 27 '22 at 11:43
  • I see the following in the console: "Uncaught SyntaxError: import declarations may only appear at top level of a module". I don't know exactly what it means. I am a beginner, so please be understanding. – Konan Destylator Apr 27 '22 at 11:46
  • I assume this is browser code. `import $ from "jquery"` isn't correct browser code. Either you use the full URL in the import statement or you use a bundler. Where does jQuery come from? Do you use npm? Do you use a script tag in the HTML code? Do you set [`type`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-type) to `module`? Import statements are only allowed in modules. Please provide a [mcve] of your project. Do you build it? If yes, how do you build it? – jabaa Apr 27 '22 at 12:00
  • The other question is: Do you really need jQuery. Of course, it's nice to understand ES6 modules, but jQuery is superfluous in most projects nowadays. You can drop jQuery and use the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to load JSON data. – jabaa Apr 27 '22 at 12:09
  • I didn't know how to load JSON data and thought to use JQuery. In that case, I will try to use the Fetch API. Thanks to everyone for your time. – Konan Destylator Apr 27 '22 at 12:15

0 Answers0