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 questionI'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 questionI'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 badges2 Answers
Reset to default 1The 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.