WordPress.org

Ready to get started?Download WordPress

Forums

jquery - not working & where to place the js code (2 posts)

  1. cleop87
    Member
    Posted 2 years ago #

    Ok, I'm driving myself nuts with jquery. I've read so much, visited the forms, and tried so many methods I feel like I'm running in circles.

    Since I can't find what I'm looking for, this may become a good tutorial for those experiencing similar issues. I'll start from scratch:

    I want to be able to use javacript files within my web pages. I don't have a blog; instead I have a web site. I use "Filezilla" and "Mozilla Firefox with Firebug 1.9.1."

    The only way I can get javascript to work is to enter the code within my individual page editor. Since nothing works, I've decided go back to the basics. This code was taken right out of "w3schools.com" and pasted into my web page. It's simply a button that, when clicked on, hides a paragraph with an ID of "test." I also installed & activated the "Use Google Libraries" plugin.

    Here's the code (& yes I know this should not be entered within the page code; I just don't know what to do next - the right way):

    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
    <script type="text/javascript" src="jquery.js">
    </script><script type="text/javascript">
    $(document).ready(function(){
      $("button").click(function(){
        $("#test").hide();
      });
    });
    </script>
    
    <h2>This is a heading</h2>
    <p>This is a paragraph.</p>
    <p id="test">This is another paragraph.</p>
    <button>Click me</button>

    Within the Header.php file, and just before the "wp_head();" I've entered this one line of code:
    wp_enqueue_script("jquery");

    Also, within the "functions.php" file I've entered:

    wp_enqueue_script('jquery');
    
    wp_enqueue_script('jquery-ui-core');
    
    wp_enqueue_script( 'onLoad', '/wp-content/themes/a-simple-love/jquery/jquery.js', array( 'jquery' ) );
    
    function add_google_jquery() {
    
       if ( !is_admin() ) {
    
          wp_deregister_script('jquery');
    
          wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', false);
    
          wp_enqueue_script('jquery');
    
       }
    
    }

    With that said, what should I do next and where (if not within the page editor) should I place the javascript code?

    Thx in advance for helping me get out of this never-ending circle. I hope it will be helpful to others as well.

  2. hotplugin
    Member
    Posted 2 years ago #

    in your functions.php

    aadd_action('init', 'hide');
    
    function hide() {
    	if (!is_admin()) {
    
    		// register your script location, dependencies and version
    		wp_register_script('hide',
    			get_stylesheet_directory_uri() . '/js/hide.js',
    			array('jquery') );
       // enqueue the script
       wp_enqueue_script('hide');
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic