nSmed nSmed - 5 months ago 9
jQuery Question

jQuery use var in a different function

Wondering if you could give me a hand, i'm pretty new to jQuery and just been messing around building a to-do list app however stuck on the delete modal. i was wondering if it is possible to get the below to work.

$(".todo-delete").click(function(){
var eld = $(this).parent();
$( ".modal" ).fadeToggle( "fast", "linear" );
});
$(".yes").click(function(){
$(eld).parent().parent().remove();
});
$(".no").click(function(){
$( ".modal" ).fadeToggle( "fast", "linear" );
});


I cant call the variable globally either as its using $(this).parent to select the item to delete providing the user presses "yes"

Thanks.

Answer

You can declare your variable earlier (global variable) and you don't need to use $(eld), it is already jQuery element:

var eld;

$(".todo-delete").click(function() {
  eld = $(this).parent();
  $(".modal").fadeToggle("fast", "linear"); 
});

$(".yes").click(function() {
  eld.parent().parent().remove(); 
});

$(".no").click(function() {
  $(".modal").fadeToggle("fast", "linear"); 
});