• Resolved johncannon

    (@johncannon)


    Hello.

    I have a website where I inserted some scripts I made, on the front page it works just fine, I loaded in <head> </head> this:

    
    <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
    

    and then I pasted my script where I wanted, it works fine.

    When I want to add the same script intro posts, with text editor, nothing happens.

    This is my script:

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    
    <p>	
    <img src="" />
    	
    <img src="" />
    </p>	
    
    <p  id="demo" > text at loading page </p>  <br>
    
    <!-- FADESCRIPT START -->
    
    <script>
    
    $('.c1').click(function(){
    $('#demo').fadeOut(0).fadeTo( "fast", 0.01 ).fadeTo( "fast", 0.05 ).fadeTo( "fast", 0.09 ).fadeTo( "fast", 1 ).html('text1');
    
    })
    
    $('.c2').click(function(){
    $('#demo').fadeOut(0).fadeTo( "fast", 0.01 ).fadeTo( "fast", 0.05 ).fadeTo( "fast", 0.09 ).fadeTo( "fast", 1 ).html('text2');
    
    })
    
    </script>
    
    <!-- FADESCRIPT  STOP -->
    
    

    It’s just a script that changes the same paragraph with a different text when you click on the image with the same class as the one from function. I am not an expert with jquery and I don’t know how to make this work in posts. Also if there is any other form so that script be written to minify, please tell me.
    I tried the solutions from Codex as well as some plugins (Javascrip in posts, Advanced Custom Fields) with no result.

    • This topic was modified 9 years, 6 months ago by Jan Dembowski.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello @johncannon,

    WordPress strips tags out of your posts and pages. If you prefer to include it, you can use plugins to include scripts. One such plugin is jQuery in Posts Pages

    If you prefer to it add it via functions.php, then this code should help you.

    
    // Reference: http://wordpress.stackexchange.com/a/151883/83739
    function wpb_adding_scripts() {
    	wp_enqueue_script( 'my_amazing_script', get_template_directory_uri() . '/removeArrows.js', array('jquery'),'0.1', true );
    } 
    add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
    

    And finally use jQuery in place of $ in your js file.

    Thread Starter johncannon

    (@johncannon)

    I tried with the jQuery in Posts Pages as well as via functions.php. It just doesn’t work, I don’t know what I am doing wrong. Exactly the same script on homepage works great.
    I have edited functions.php and replaced “my_amazing_script” with the URL http://…etc/filename.js where I pasted that script with jQuery instead of $.

    • This reply was modified 9 years, 6 months ago by johncannon.

    @johncannon,

    In the following line, my_amazing_script is the handle for your script. You have replace the src param.

    wp_enqueue_script( 'my_amazing_script', get_template_directory_uri() . '/removeArrows.js', array('jquery'),'0.1', true );

    Code Reference

    wp_enqueue_script( string $handle, string $src = false, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

    Also, once you include it in your functions.php, check if the script has been included in your page by viewing the page source in your browser.

    Thread Starter johncannon

    (@johncannon)

    It works, thanks.

    @johncannon,

    Glad my answer helped. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Javascript in posts’ is closed to new replies.