Is there any way to get the ID of the element that fires an event?
I'm thinking something like:
<html>
<head>
<script type="text/javascript" src="starterkit/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a").click(function () {
var test = caller.id;
alert(test.val());
});
});
</script>
</head>
<body>
<form class="item" id="aaa">
<input class="title"></input>
</form>
<form class="item" id="bbb">
<input class="title"></input>
</form>
</body>
</html>
Except of course that the var test
should contain the id "aaa"
, if the event is fired from the first form, and "bbb"
, if the event is fired from the second form.
You can use
(this)
to reference the object that fired the function.'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.Here is where you can learn more: http://remysharp.com/2007/04/12/jquerys-this-demystified/
You can try to use:
I'm working with
I tried looking for an
event
as described above, but when the request function fires it doesn't seem to be available. I usedthis.element.attr("id")
to get the element's ID instead, and it seems to work fine.this works with most types of elements:
The source element as a jQuery object should be obtained via
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 EG.a click event on a table row, and you need the cell that was clicked
Use can Use .on event