I have a newbie question about jQuery. Why is it that I can trigger the click event below even though the element has changed class? Even though I change the class from “edit” to “tide”, $(“.edit”).click() is working on that element.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".edit").click(function(){
console.log(this);
$(this).removeClass("edit").addClass("tide");
});
});
</script>
</head>
<body>
<div class="edit">Hi there</div>
</body>
</html>
Thank you!
If you want that behavior, use
.live(), like this:You can test it out here. As it is now,
$(".edit")finds elements that match the.editselector at that time and binds to them, thoseclickevent handlers are in no way dependent upon the class name after that. With.live()the selector is evaluated at the time of the event, so it does matter.