• Subject: jQuery – How to test a jQuery script
    Wordpress Version: 3.3.1
    Theme: A simple Love (child theme for “Thematic”

    I’m trying desperately to use jQuery. I’ve read and tried tons of jQuery coding but can’t get it to work.
    I’m trying to test whether or not my jQuery library is working – right now it isn’t. I’ve created a new page in my theme I’ve entered a very simple test that shows a paragraph and when that paragraph is clicked on, an “alert” box shouldl open and display “Works.” So here’s the code:

    I’m using an “onclick” function that will show an “alert” box when one clicks on the paragraph. So, first I add the paragraph and include the ‘onclick’ event:
    <p onclick=“alert (‘Works’);”>This is a paragraph</p>

    Last, I add the jQuery code (within the new page):
    <script type=”text/javascript” src=”https://giftsrus.org/wp-includes/js/jquery/jquery.js”></script>

    What is supposed to happen is a paragraph will be displayed (no problem there) and when clicked on the alert box displays “Works.” However, nothing happens when I click on the paragraph. I have several jQuery scripts to add but can’t even get the most simplest of code to work. I think the issue might be in my ‘src’ code for jQuery within my sight but I’m not sure. I’m not exactly sure where the jQuery library is and how to reference it.

    Any help would be greatly appreciated.
    Thx.
    Cleop87

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi, WordPress has Jquery built into it. Try calling it in header.php and not in the page. You must have it on the theme’s code.Just open up header.php in your theme folder and add the below code just before <?php wp_head(); ?>

    Don’t include Jquery from your source.Just use this code and Jquery will be called in automatically.
    <?php wp_enqueue_script('jquery'); ?>

    Also do not use scripts from within the page editor. You must have it on code. Look at this good tutorial about using Jquery with WordPress and you should be good to go:
    https://wpmu.org/using-jquery-with-wordpress-a-quick-crash-course-and-performance-tips/

    First I’m not an Javascript/JQuery expert ?? , though my webdevelopment always contains JQuery code .
    While onClick is a Javascript event handler , JQuery is a Framework that sits on top of Javascript and use different DOM traversing functions .
    Take a look at JQuery’s library for excellent code samples (.click example) .

    Thread Starter cleop87

    (@cleop87)

    Hello Abinav,

    Thank you so very much for the quick reply. I followed your suggestion and read the link article. It was quite helpful.

    Then I installed and activated the plugin “Use Google Libraries.” I can now see this code in the head profile via Mozilla Firefox:

    <script type=’text/javascript’ src=’https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js’></script&gt;

    To test whether or not it works, I’ve created a new text page and entered the following code(within the page template):

    <script type=’text/javascript’ src=’https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js’></script&gt;

    <script type=”text/javascript”>
    $(document).ready(function(){
    $(“button”).click(function(){
    $(“p”).hide();
    });
    });
    </script>

    <h2>This is a heading</h2>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <p>When you click on the “Click Me” button it should hide these 3 paragraphs and show only the “Heading.”
    <button>Click me</button>

    Amazingly, it worked like a champ; but Since you mentioned that we should not enter the code within the page template, where should the “java scripts” be entered? I fought with it this weekend but could only get it to work within the page template.

    Any suggestions would be greatly appreciated. Thanks again for your help.

    Kindest regards,
    Cleop87

    Thread Starter cleop87

    (@cleop87)

    Hi kostas123,

    Thanks a bunch for your quick reply. I don’t know how to use “DOMs” so I’ll have to add that to my to-do list. I’m following your suggestion and looking at the libraries.

    Again, thanks for the tips – every little bit helps.

    Kindest regards,
    cleop87

    Thread Starter cleop87

    (@cleop87)

    Still trying to get this to work; not successful.
    Using WP version 3.3.1.
    Theme = “a-simple-love (thematic child theme

    I’ve seen this tip from the WP forum and added it to my function.php file:
    <?php
    wp_enqueue_script(‘jquery’);
    wp_enqueue_script(‘jquery-ui-core’);
    wp_enqueue_script( ‘onLoad’, ‘/wp-content/themes/my-theme/js/onLoad.js’, array( ‘jquery’ ) );
    ?>

    I deactivated my “Use Google Libraries” plugin. It wasn’t working either – unless it was entered in the page via the new page in the dashboard.

    So I’m back to hard-coding in the functions.php file and the header.php file. In the header.php file I’ve entered (before the “wp_head ();” code). I’ve worked weeks on this to no avail.

    This is the src code when using the “Use Google Libraries” plugin. Should this be entered some place?

    <script type=’text/javascript’ src=’https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js’></script&gt;

    Still not working. Any help Please.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘jQuery – How to test a jQuery script’ is closed to new replies.