I have a toolbar that manipulates a Google map. Some of the functions affect markers placed on the map. I have a reset button that clears the changes and returns the map to the original state.
I need to “disable” the reset button until the map is changed. So…
- When an action is completed that manipulates the map, I bind the ResetMap() function to the reset button.
- Reset button is clicked and returns the map to original state.
- Reset button action is unbound again.
However, I can’t get step 3 to work. I tried to unbind the event at the end of the ResetMap() function like so:
function ResetMap(){
var Wrapper = $("#page_layout").find("#InlineToolbarMessages");
if(Wrapper.hasClass('isExpanded')){
Wrapper.removeClass('isExpanded');
Wrapper.addClass('isCollapsed');
}
$("#page_layout").find('#NumSelectedNumber').html("0");
$("#page_layout").find('div#SelectedVoters').html("");
clearMarkers();
$('#page_layout').find('#reset_button').unbind('click', function(){
ResetMap();
});
}
I also tried a more global:
$('#page_layout').find('#reset_button').unbind();
without success. What am I doing wrong?
Edit: Based on your comment that you “…found out that if [you] did
$('#reset_button').unbind('click');before [you] called the `ClearMarkers()“ function that it worked. Similarly in another function for a different button, [you] put the unbind at the beginning and it worked.”:That makes me think that either A) The code earlier is throwing an exception, preventing your getting to the
unbindcall at all, or B) The code earlier is removing thereset_buttonbutton or adding another one with the same ID (which will make the document invalid and the behavior of an ID selector indeterminate).Original Answer
Change
to
…assuming you used
ResetMapin thebindcall. Since you haven’t shown thebind, it’s hard to say for certain.When you call
unbindwith a function reference, it has to be the exact same function reference that you gavebind(or one ofbind‘s shortcuts,clickand such).So for instance, this works:
…but this does not work:
…because the function references are not the same.