Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save jquery selector for later use

I have the following code...

$('#item_' + code + ' .delete').hide();
$('#item_' + code + ' .deleting').show();
$('#item_' + code).slideUp(400, function() {
    $(this).remove();
    $('#top .message').html('Item has been deleted');
});

I want to save the selector I'm using in a variable and use it to perform operation instead of searching the DOM everytime.

So I save the selector like this...

 var saved = $('#item_' + code);

But how do I change the rest of the code? I'm not very familiar with jQuery, hence wondering how this can be done. Will this work...

$(saved).(' .delete').hide();
$(saved).(' .deleting').hide();
$(saved).slideUp(400, function() {
    $(this).remove();
    $('#top .message').html('Item has been deleted');
});
like image 311
vikmalhotra Avatar asked Feb 18 '11 05:02

vikmalhotra


1 Answers

I'll add another alternative

$('.delete', saved).hide();
$('.deleting', saved).show()
...
like image 80
Vadim Avatar answered Sep 28 '22 10:09

Vadim