javascript - Select previous "this" element -


i have piece of javascript code trying previous element declared. have 2 onclick functions within eachother.

$('#formingredient').on("click", '.newingredient', function() {     value = $(this).attr('data-name');     $(this).attr('data-isactive', 'true');      $('#newingredientinput').val(value);     $('#newingredient').modal('show')      $('#createnewingredient').click(function() {         inputname = $('#newingredientinput').val();         inputcategory = $('#newingredientcategory').val();          var newingredient = $.ajax({             type: "get",             url: '/content/includes/ajax/createnewingredient.php',             data: {name: inputname, id_category: inputcategory}         });          newingredient.done(function(result) {             //pai.showpage(pai['page']);             $(this).parent().replacewith('hello');             $('#newingredient').modal('hide');         });     }); }); 

i trying use previous element code.

$(this).parent().replacewith('hello'); 

any ideas?

the scope of $(this) seems issue here... need correct code.

$('#createnewingredient').click(function() {     var self = this; // #createnewingredient element      inputname = $('#newingredientinput').val();     inputcategory = $('#newingredientcategory').val();      var newingredient = $.ajax({         type: "get",         url: '/content/includes/ajax/createnewingredient.php',         data: {name: inputname, id_category: inputcategory}     });      newingredient.done(function(result) {         //pai.showpage(pai['page']);         // $(this).parent().replacewith('hello'); // $(this) scope lost current function...         $(self).parent().replacewith('hello'); // scope need         $('#newingredient').modal('hide');     }); }); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -