I have an HTML button with an ID of "open". I have added a jQuery .click() binding to the HTML button which is selected by ID. In the .click() binding I change the ID of "open" to "close". However, subsequent clicks on the "open" button are still firing even though the ID has been changed to "close". The code is as follows:
index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<button id="open">Open</button>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
index.js
$('#open').click(function() {
console.log("clicked");
$(this).attr('id', 'close');
});
https://jsfiddle.net/iHexBot/28tj1ywg/
I was expecting / hoping to see the console log "clicked" only one time. However, it logs "clicked" every time the button is clicked even though the HTML elements ID is no longer "open". Could someone please explain to me why this happens and if possible, how to fix this?