• Hi,

    I’m trying to use the jQuery code I found on CodePen on my project but all methods I’ trying are not working.
    I’m using @wordpress/scripts to bundle all my files. And all java script is loaded as a modules.

    This is my folders hierarchy:

    
    --build
    --css
      |--base
      |--modules
      |--style.scss
    --img
    --src
      |--modules
      |   |--Accordion.js
      |   |--StickyHeaders.js
      |--index.js
    --template-parts
    --functions.php
    --package.json
    --webpack.config.js
    --all-pages.php
    

    My code is inside Accordion.js file and looks like this:

    import $ from 'jquery';
    
        var headers = ["H1","H2","H3","H4","H5","H6"];
    
        $(".accordion").click(function(e) {
        var target = e.target,
               name = target.nodeName.toUpperCase();
    
        if($.inArray(name,headers) > -1) {
            var subItem = $(target).next();
    
            //slideUp all elements (except target) at current depth or greater
            var depth = $(subItem).parents().length;
            var allAtDepth = $(".accordion p, .accordion div").filter(function() {
            if($(this).parents().length >= depth && this !== subItem.get(0)) {
                return true;
            }
            });
            $(allAtDepth).slideUp("fast");
    
            //slideToggle target content and adjust bottom border if necessary
            subItem.slideToggle("fast",function() {
                $(".accordion :visible:last").css("border-radius","0 0 10px 10px");
            });
            $(target).css({"border-bottom-right-radius":"0", "border-bottom-left-radius":"0"});
        }
        });

    At my main JS file which is index.js I’m importing this file like below:
    import Accordion from './modules/Accordion'

    But the code is not working. I tried to wrap it within the function like below but also no luck:

    
    import $ from 'jquery';
    
    $(document).ready(function() {
    
     var headers = ["H1","H2","H3","H4","H5","H6"];
    
        $(".accordion").click(function(e) {
        var target = e.target,
            name = target.nodeName.toUpperCase();
    
        if($.inArray(name,headers) > -1) {
            var subItem = $(target).next();
    
            //slideUp all elements (except target) at current depth or greater
            var depth = $(subItem).parents().length;
            var allAtDepth = $(".accordion p, .accordion div").filter(function() {
            if($(this).parents().length >= depth && this !== subItem.get(0)) {
                return true;
            }
            });
            $(allAtDepth).slideUp("fast");
    
            //slideToggle target content and adjust bottom border if necessary
            subItem.slideToggle("fast",function() {
                $(".accordion :visible:last").css("border-radius","0 0 10px 10px");
            });
            $(target).css({"border-bottom-right-radius":"0", "border-bottom-left-radius":"0"});
        }
        });
    });

    I’ve to also tried to import jQuery library inside my .php file within <script> tags like below but that is not changing anything:
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

    I don’t know jQuery so hope someone can help me here.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator t-p

    (@t-p)

    Thread Starter maryando

    (@maryando)

    Thank you so much for your answer, I tried this already and none of those methods works.
    My scripts are bundled into index.js and this file is enqueued in my function.php files so all setup works as I can see the jQuery code inside index.js file compiled by web pack and it looks like this:

    /***/ "./src/modules/Accordion.js":
    /*!**********************************!*\
      !*** ./src/modules/Accordion.js ***!
      \**********************************/
    /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
    
    "use strict";
    __webpack_require__.r(__webpack_exports__);
    /* harmony import */ var jquery__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! jquery */ "jquery");
    /* harmony import */ var jquery__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(jquery__WEBPACK_IMPORTED_MODULE_0__);
    
    jQuery(document).ready(function ($) {
      var headers = ["H1", "H2", "H3", "H4", "H5", "H6"];
      $(".accordion").click(function (e) {
        var target = e.target,
            name = target.nodeName.toUpperCase();
    
        if ($.inArray(name, headers) > -1) {
          var subItem = $(target).next(); //slideUp all elements (except target) at current depth or greater
    
          var depth = $(subItem).parents().length;
          var allAtDepth = $(".accordion p, .accordion div").filter(function () {
            if ($(this).parents().length >= depth && this !== subItem.get(0)) {
              return true;
            }
          });
          $(allAtDepth).slideUp("fast"); //slideToggle target content and adjust bottom border if necessary
    
          subItem.slideToggle("fast", function () {
            $(".accordion :visible:last").css("border-radius", "0 0 10px 10px");
          });
          $(target).css({
            "border-bottom-right-radius": "0",
            "border-bottom-left-radius": "0"
          });
        }
      });
    });

    there is no errors in DevTools, module is exported and injected to index.js file correctly but the code is not working. It works when moved to CodePen but not in my theme ??

    • This reply was modified 2 years, 8 months ago by maryando.
    • This reply was modified 2 years, 8 months ago by maryando.
    Thread Starter maryando

    (@maryando)

    I have noticed that when scripts are enqueued directly in functions.php all works perfectly. When the scripts is exported in main.js and bundled by @wordpress/scripts then the jQuery is not working. Is there any issue with @wordpress/scripts then? Actually I’ve pasted above how jQuery is bundled and I don’t see anything wrong there. Looks like is bundled properly but somehow it doesn’t work.

    • This reply was modified 2 years, 8 months ago by maryando.
    • This reply was modified 2 years, 8 months ago by maryando.
    • This reply was modified 2 years, 8 months ago by maryando.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘jQuery and WP custom theme is not working’ is closed to new replies.