• I am trying to figure out how to link a custom jquery to my custom meta box created in wordpress custom post type but I can’t find it working.

    What I want to do is add a line of text when the button is clicked. But it is only reload the “add new post” page and generating a message “Post updated.” after I clicked the button.

    Here is my code:

    File: doubleit.js

    jQuery(document).ready(function($){
      $("#btn1").click(function(){
        $("p").append(" <b>Appended text</b>.");
      });
    
      $("#btn2").click(function(){
        $("ol").append("<li>Appended item</li>");
      });
    });

    Register and Enqueue Script

    add_action( 'admin_enqueue_scripts', 'add_mytweetjs' );
    
    function add_mytweetjs() {
    
        wp_deregister_script('jquery');
    
        wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
    
        wp_register_script( 'myjquerydoubler', plugins_url( '/js/doubleit.js', __FILE__ ), array( 'jquery' ) );
    
        wp_enqueue_script( 'myjquerydoubler' );
    }

    HTML Script On The Custom Meta Box

    echo '<p>This is a paragraph.</p>';
    echo '<p>This is another paragraph.</p>';
    echo '<ol>';
    echo '<li>List item 1</li>';
    echo '<li>List item 2</li>';
    echo '<li>List item 3</li>';
    echo '</ol>';
    echo '<button id="btn1">Append text</button>';
    echo '<button id="btn2">Append list items</button>';

    May be somebody know where is the fault of my code above.

  • The topic ‘I can't link my custom jquery to wp admin’ is closed to new replies.