最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught ReferenceError: jQuery is not defined - although jQuery is loaded

programmeradmin2浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 2 years ago.

Improve this question

I'm aware that this is a well discussed question, nevertheless I did not find any answer for my issue. I have the following code on a Wordpress page (inline, necessary for this project):

<script>
(function ($) {
    $(document).ready(function () {
    $('.choosekurs').on('click', function() {
        $('textarea[name="textarea"]').text($(this).attr('data-attr-package')); 
     });
})(jQuery);
</script>

I also tried it the other way around:

<script>
jQuery(document).ready( function($){
    $('.choosekurs').on('click', function() {
        $('textarea[name="textarea"]').text($(this).attr('data-attr-package')); 
  });
});
</script> 

Unfortunately I still get the same error. I can see jQuery being loaded in the footer (v3.6.0 as well as 3.3.2 migrate version), so I'm not really sure what I'm doing wrong. Any help is greatly appreciated!

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 2 years ago.

Improve this question

I'm aware that this is a well discussed question, nevertheless I did not find any answer for my issue. I have the following code on a Wordpress page (inline, necessary for this project):

<script>
(function ($) {
    $(document).ready(function () {
    $('.choosekurs').on('click', function() {
        $('textarea[name="textarea"]').text($(this).attr('data-attr-package')); 
     });
})(jQuery);
</script>

I also tried it the other way around:

<script>
jQuery(document).ready( function($){
    $('.choosekurs').on('click', function() {
        $('textarea[name="textarea"]').text($(this).attr('data-attr-package')); 
  });
});
</script> 

Unfortunately I still get the same error. I can see jQuery being loaded in the footer (v3.6.0 as well as 3.3.2 migrate version), so I'm not really sure what I'm doing wrong. Any help is greatly appreciated!

Share Improve this question asked Feb 24, 2022 at 22:12 ParanoiaParanoia 252 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The error is self-explanatory. When you insert your code, jQuery has not been defined. You have to put that script after jQuery is loaded.

If you can't change where jQuery is defined, as you say in your comment to Pat J's answer, then insert your script in the <footer>.

If the inline <script> is in the <head> or <body> of your page, and jQuery is loaded in the footer (ie, at the end), then jQuery doesn't exist when you're trying to run your jQuery code.

Either enqueue jquery to load in the header (ie, set the $in_footer parameter to false), or—the better option—use wp_enqueue_script() to properly load your script. This latter option will also let you define jquery as a dependency for your script.

发布评论

评论列表(0)

  1. 暂无评论