0

Gentlemen!

I'm composing a theme options page for my WP theme. And I bumped into the problem of finding the proper way to introduce inline jQuery interactions to the page.

Ideally for me in the situation will be inline jQ code

<script type="text/javascript">
    $(document).on({
        ready: function(){
            alert('Hey!');
        }
    })
</script>

But this code fails because I guess the admin options page is rendered before the jQuery is included so js generates expected error. The same code with no jQ identifiers works ok means I get alert window when introduce:

<script type="text/javascript">
     alert('Hey!');
</script>

Question is how to properly introduce inline js containing jQ (or any other framework) syntax and make it work?

5
  • Why do you want to use inline JavaScript and not include a JavaScript file? And if you have a reason to use inline
    – engelen
    Commented Jun 11, 2014 at 12:55
  • Hi I need to use some values stored in WP database. Something like alert('<? echo get_option("some-stuff"); ?>');
    – LoomyBear
    Commented Jun 11, 2014 at 12:57
  • Does your code generate JavaScript errors (check using the developer console)? It could be a conflict with another library over $. Have you tried jQuery( document )?
    – engelen
    Commented Jun 11, 2014 at 12:58
  • 1
    jQuery(...) did the trick! Thanks!! Can you publish it as an answer so I can mark it as helpful? Cheers!!
    – LoomyBear
    Commented Jun 11, 2014 at 13:01
  • No problem! I've just added it as an answer!
    – engelen
    Commented Jun 11, 2014 at 13:04

1 Answer 1

2

The problem with your code is, as we found out in the comments on the question, that you're using $, which is either undefined or has a conflict (in any case, it's not pointing to jQuery).

Using jQuery( document ) instead of $( document ) should do the trick!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.