0

I'm working in a jQuery plugin and tried to trigger a custom event after insert a jQuery Dom Element in body.. but the events never occurs..

Is this possible without user interact?

My code is:

var $div = $( '<div />' ), $container = $div.clone();

$container.text( 'test' ).appendTo( 'body' );
$.event.trigger( 'ContainerInit', [ $container ]);

$( document ).on( 'ContainerInit', function( e, $elementContainer ) {
   alert( 'test here!' );
});

1 Answer 1

1
$(document).ready(function() {
    var $div = $('<div />'),
               $container = $div.clone();

    $container.text('test').appendTo('body');

    // you should provide a selector to the handler!
    $(document).on('ContainerInit', 'div', function(e, $elementContainer) {
       // console.log is better than alert
       console.log('test here!');
    });

    // trigger event only after assigning handler using correct way to trigger!
    $container.trigger('ContainerInit', [$container]);
});

check here

Sign up to request clarification or add additional context in comments.

1 Comment

You should add explanation along with your code, otherwise it may not be clear to OP what you actually changed and why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.