S.M_Emamian
S.M_Emamian

Reputation: 17393

how to fire event on class change by manual?

how can I do trigle .delivery to fire change function ?

 $(document).ready(function(e) {

     $('.delivery').change(function(e) {
         var total = 0 ;
         var price = 0 ;
         ....

Upvotes: 1

Views: 971

Answers (3)

Alexander Solonik
Alexander Solonik

Reputation: 10260

How about a custom Event eg.

$('#myelement').trigger($.Event('divchange.namespace'));

and you can listen for it , like so:

 $(document).on('divchange.namespace' , '#myelement', function(){
  // do something
 });

If all you want to know , is how to fire a event manually , use trigger().

Upvotes: 0

Jai
Jai

Reputation: 74748

The question been asked seems to be half because you didn't add the added class name here. If you are just adding the class name to the target element then you just have to do this:

$('.delivery').change();  // <---will trigger the change.

If you are removing the old class then you have to rethink the code. Then you have to make a function and call it like a callback:

function firechange(){
    console. log(event.type);
    // put the code for change event
}

$('.delivery').change(firechange);

Now when you remove and add a new class then after adding the class you have to bind the change event because this class doesn't hold any event. This class has been added to the DOM node after page load.

Suppose you added a class newclass and removed the old one from the element:

    $('.delivery').removeclass('delivery')
                          .addclass('newclass');

    $('.newclass').change(firechange).change(); // <--bind here and trigger.

Or this way:

    $('.delivery').removeclass('delivery')
                          .addclass('newclass')
                          .change(firechange);

    $('.newclass').change(); // <--bind here and trigger.

Upvotes: 3

itamar
itamar

Reputation: 3967

Like this:

$('.delivery').trigger('change');

Upvotes: 3

Related Questions