Instead of a global variable, you can use the jQuery data () method to bind information to the document itself:
$(".objects_list").live('click', function(event) { $(this).css("color", "blue"); $(document).data("yourObjectKey", $(this)); });
Then you can easily get this information later:
$("otherSelector").click(function() { var yourObject = $(document).data("yourObjectKey"); if (yourObject != null) { yourObject.css("color", "red"); } });
EDIT: If an element is destroyed and recreated between two events, this method will not work. In this case, you can save the id element instead of referencing the element itself:
$(".objects_list").live('click', function(event) { $(this).css("color", "blue"); $(document).data("yourObjectKey", this.id); });
Then:
$("otherSelector").click(function() { var yourObjectId = $(document).data("yourObjectKey"); if (yourObjectId != null) { $("#" + yourObjectId).css("color", "red"); } });
source share