I'm working on a Wordpress plugin and a I have to use jquery. In my plugin file (call it 'my_p.php') I register my .js file (my_js.js) in this way:
add_action('admin_enqueue_scripts', 'my_script');
function my_script() {
wp_register_script( 'my_jquery', plugins_url( '/js/my_js.js', __FILE__ ), array( 'jquery' ), '1.0.0', false );
wp_enqueue_script( 'my_jquery' );
}
Code in my_js.js is:
jQuery(document).ready(function($){
jQuery(document.body).on('click','#button_my_p', function(e){
alert('hello!');
});
});
my problem is the following:
if 'button_my_p
' is an element (in my case a button) in *my_p.php* file, it works fine and I see the alert when I click on the button. But, if 'button_my_p
' is an element of another .php file (for example 'other.php' in my plugin folder), jquery code doesn't work.. I don't see the alert..
What am I missing???
My plugin's structure is:
- js folder in which I put my_js.js.
- my_p.php file
- other.php file
Thanks for your help!