Is there any way that I can get the id of an element from something like:
<a href="#" class="test" id="test_1">Some text</a>
<a href="#" class="test" id="test_2">Some text</a>
<a href="#" class="test" id="test_3">Some text</a>
and then I bind
$('.test')
so when I click one of the elements I get back the id?
Doh.. If I get you right, it should be as simple as:
$('.test').click(function() {
console.log(this.id);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="test" id="test_1">Some text</a>
<a href="#" class="test" id="test_2">Some text</a>
<a href="#" class="test" id="test_3">Some text</a>
You can just access the id property over the underlaying dom node, within the event handler.
Use "attr" method in jquery.
$('.test').click(function(){
var id = $(this).attr('id');
});
When you add a click event, this returns the element that has been clicked. So you can just use this.id;
$(".test").click(function(){
alert(this.id);
});
Example: http://jsfiddle.net/jonathon/rfbrp/