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 the function to get the id and the value for the changed item(in my example, I've used a Select tag.
For all events, not limited to just jQuery you can use
Where
event.target
fails it falls back onevent.srcElement
for IE. To clarify the above code does not require jQuery but also works with jQuery.This works on a higher
z-index
than the event parameter mentioned in above answers:This way you will always get the
id
of the (in this exampleli
) element. Also when clicked on a child element of the parent..Both of these work,
and
For reference, try this! It works!
Working JSFiddle here.