Reputation: 39
I have some code, loaded by AJAX. Like this:
<a href="#" class="selectclass" id="seltest" value="1234">Some text</a>
<a href="#" class="selectclass" id="seltest" value="9876">Some text2</a>
I want get ID of element,then click.
I try
$(document).on('click', "#seltest", function (e) {
alert($(e.target).val());
});
but in alert is nothing..
What is the problem?
Upvotes: 0
Views: 37
Reputation: 171689
You can't repeat ID's in a page, they are unique by definition , so use class selector instead .
Also <a>
elements have no value
so val()
is useless . Use a data attribute
$(document).on('click', "a.selectclass", function(e) {
e.preventDefault()
console.log($(this).data('value'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="selectclass" data-value="1234">Some text</a>
<a href="#" class="selectclass" data-value="9876">Some text2</a>
Upvotes: 1