When you change the id
on an element (which arguably isn't a good thing to do anyway, but that's beside the point), you don't change the event handlers that are bound to it. The same click handler is still on that element.
Instead of binding to the elements, use event delegation and bind to a common parent. Then you don't need to change the handler because the identity of the element will follow the click event. Something like this:
$(document).on('click', 'a#showdiv', function () {
// your code
});
$(document).on('click', 'a#hidediv', function () {
// your code
});
As the id
changes, any time document
receives the click event it will examine the current id
of the element which invoked the event, so it will use the correct handler.
The main thing to learn here is that while the code inside the click handler runs any time the element is clicked, the code which assigns the click handler ($('something').click(someFunction);
) runs only once when the page initially loads, and it assigns the handler to elements which match the selector at that time only.