by andrew

jQuery event object

This post is copy of a part of previous post. I’ve just started to adding new updates to the previous one and finally decided to make it a different one What is it about? It’s about something most of you know if you work a lot with jQuery or if you read its documentation more carefully than I did. It’s about an interesting property in jQuery event object and some interesting results of using jQuery on() method.

If you want to bind a function to click event of an element it is quite easy if the element exist in DOM after page is loaded. For example:

Maybe I will play with it more and write more about it later because right now I did not even check if the code I had written above is correct

Update (2012-06-07):
I put the code above to a test file and it works. However, I hadn’t expected it to work the way it worked I expected e.target and e.currentTarget to be different when I used jQuery on() method. At the begining I thought it worked correctly because the code didn’t change DOM anyhow. So, I created another test file with DOM modifications. Again — the results weren’t as I had expected them to be It was like that during our work I mentioned above. Maybe the HTML there was more complicated that one from my examples and that was the case. I’ll investigate it more and if find out what is going on, I’ll update it here

Update (2012-06-16):
The same day I wrote previous update I talked about the issue with my colleague. He had showed me the issue during our project work. He agreed with me our code was a lot more complicated than my examples. It was a wrapper with an image inside and unsorted list with anchors inside. Clicking on one of the anchor was supposed to trigger an event which changed the image. It took me few time to create another example. I’ve just added a wrapper then and bind an event to it. Clicking buttons and observing what’s going on in the console. It illustrate our issue a little bit.