TransWikia.com

jQuery Click event is not fired at all

WordPress Development Asked by Islam Hanafi on October 30, 2021

I’m trying to make a widget plugin that allows user to upload image. but I have a problem in jQuery events that it is never happen !

includes/myplugin.scripts

function media_uploader_enqueue() {
    wp_enqueue_media();
    wp_register_script('media-uploader', plugins_url().'/simple-plugin/js/main.js',array('jquery'),null, true);
    wp_enqueue_script('media-uploader');
}
add_action('admin_enqueue_scripts', 'media_uploader_enqueue');

js/main.js

jQuery(document).ready(function ($) {
    console.log($('#xyz')); // works !

    $('#xyz').click(function (e) {
        e.preventDefault();
        console.log("in click event");  // never happens !!
    })

})


One Answer

I Found the answer for my question , And I'm sharing it here in case anyone interested.

the problem was that click event won't work on dynamically generated html elements

and the alternative that worked is using on

$('body').on('click','#elementID',Callback)

Answered by Islam Hanafi on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP