I am trying to teach myself jQuery and am currently goofing around with click events. As I understand it the syntax for firing a click event based on id is
$("#IDname").click(function(){ //stuff goes here})
When I try this the click event does not fire. However when I change the "#IDname" to use 'this' object the click event DID fire. I don't think I want to fire based of 'this' object and I am still playing with the code to get the handler to fire the way I expect (or until I find that I am looking at the problem all wrong).
My question is why did the event fire with 'this' but not by ID. See my sample code below:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<style>
.heading {
font-family:"Palatino Linotype", "Book Antiqua", Palatino, serif;
}
</style>
</head>
<body>
<script src="jquery.js"></script>
<script>
$( document ).ready(function() {
<!-- Does not fire the click event -->
$( "#shopping_list" ).click(function(event) {
$("#shopping_list").append("<tr>");
$("#shopping_list").append("<td>Doe</td>");
$("#shopping_list").append("<td>Re</td>");
$("#shopping_list").append("<td>Mi</td>");
$("#shopping_list").append("</tr>");
})
<!-- Works as expected...this adds a table row when the 'Shopping List' text is clicked -->
$( this ).click(function(event) {
$("#shopping_list").append("<tr>");
$("#shopping_list").append("<td>1</td>");
$("#shopping_list").append("<td>2</td>");
$("#shopping_list").append("<td>3</td>");
$("#shopping_list").append("</tr>");
})
});
</script>
</body>
<div class="heading"><h2>Shopping List</h2>
<div class="shopping_list">
<table>
<tbody>
<form>
<div id="shopping_list"></div>
</form>
</tbody>
</table>
</div>
</div>
</html>