jquery - Why is $(this).data('id') not working when using in a dialog? -


i had function remove item when delete-item button clicked:

$( document ).on( "click", ".delete-item", function() {                      console.log("removing "+$(this).data('id'));                     var id=$(this).data('id');                     var parent=$(this).parent().parent().parent();                           parent.remove();                     $(".subitem-row"+id).remove();                     applycartchanges();  }); 

it works fine. when try dialog (using onsenui , phonegap) confirm first before removing, this:

$( document ).on( "click", ".delete-item", function() {          ons.notification.confirm({           message: gettrans('remove cart?','remove_from_cart') ,                title: dialog_title_default ,           buttonlabels: [ gettrans('yes','yes') ,  gettrans('no','no') ],           animation: 'default', // or 'none'           primarybuttonindex: 1,           cancelable: true,           callback: function(index) {                 if (index==0){                                               console.log("removing "+$(this).data('id'));                     var id=$(this).data('id');                     var parent=$(this).parent().parent().parent();                           parent.remove();                     $(".subitem-row"+id).remove();                     applycartchanges();                 }           }         });  }); 

then doesn't work anymore :( in console, says undefined $(this).data('id'). ideas why?

this because reference this not obtained clicked element inside dialog. so, rewrite code

$( document ).on( "click", ".delete-item", function() {         var _this = this;         ons.notification.confirm({           message: gettrans('remove cart?','remove_from_cart') ,                title: dialog_title_default ,           buttonlabels: [ gettrans('yes','yes') ,  gettrans('no','no') ],           animation: 'default', // or 'none'           primarybuttonindex: 1,           cancelable: true,           callback: function(index) {                 if (index==0){                                               console.log("removing "+$(_this).data('id'));                     var id=$(_this).data('id');                     var parent=$(_this).parent().parent().parent();                           parent.remove();                     $(".subitem-row"+id).remove();                     applycartchanges();                 }           }         });  }); 

notice have taken reference of this inside new variable _this accessible inside dialog.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -