tag:blogger.com,1999:blog-865923359735383241.post2081108413643555077..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: Getting the ID of the element that fired an event using JQueryUnknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-865923359735383241.post-21287340009300596812012-06-11T22:10:33.326-06:002012-06-11T22:10:33.326-06:00The source element as a jQuery object should be ob...The source element as a jQuery object should be obtained via<br /><br />var $el = $(event.target);<br /><br /><br />This gets you the source of the click, rather than the element that the click function was assigned too. Can be useful when the click event is on a parent object<br />EG.a click event on a table row, and you need the cell that was clicked<br /><br />$("tr").click(function(event)<br />{<br /> var $td = $(event.target);<br />});Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-65498412677025340542012-06-11T22:10:32.259-06:002012-06-11T22:10:32.259-06:00I generate a table dynamically out a database, rec...I generate a table dynamically out a database, receive the data in JSON and put it into a table. Every table row got a unique ID, which is needed for further actions, so, if the DOM is altered you need a different approach:<br /><br />$("table").delegate("tr", "click", function() {<br /> var id=$(this).attr('id');<br /> alert("ID:"+id); <br />});Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-19298729824429505992012-06-11T22:10:30.971-06:002012-06-11T22:10:30.971-06:00I'm a tad late. lol. For reference, try this! ...I'm a tad late. lol. For reference, try this! It works!<br /><br />jQuery("classNameofDiv").click(function() {<br /> var contentPanelId = jQuery(this).attr("id");<br /> alert(contentPanelId);<br /><br /> });Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-78688145862851556832012-06-11T22:10:30.004-06:002012-06-11T22:10:30.004-06:00You can use (this) to reference the object the fir...You can use (this) to reference the object the fired the function.<br /><br />'this' is a DOM element when you are inside of a callback function (in the context of jQuery), for example, being called by the click, each, bind, etc. methods.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-12399660548762828742012-06-11T22:10:29.181-06:002012-06-11T22:10:29.181-06:00In jQuery event.target always refers to the elemen...In jQuery event.target always refers to the element that triggered the event, where 'event' is the parameter passes to the function. http://docs.jquery.com/Events_(Guide)<br /><br />$(document).ready(function() {<br /> $("a").click(function(event) {<br /> alert(event.target.id);<br /> });<br />});<br /><br /><br />Note also that 'this' will also work, but that it is not a jQuery object, so if you wish to use a jQuery function on it then you must refer to it as '$(this)', e.g.:<br /><br />$(document).ready(function() {<br /> $("a").click(function(event) {<br /> // this.append wouldn't work<br /> $(this).append(" Clicked");<br /> });<br />});Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com