Reputation: 2286
simple bit of jquery. I have a div with clss linklist, inside it has several linkbuttons
<script type="text/javascript">
$(function () {
$('#linklist a').each(function () {
alert(this.text);
});
});
</script>
I would assume that it should alert the text inside each rendered hyperlink. instead I am getting the value "undefined"
if i change it to
alert(this.id);
i am getting the correct client id - therefore i know at least that I am selecting correct. why is this value undefined? same goes for this.text and this.value
Thanks
Upvotes: 0
Views: 179
Reputation: 422
$(document).ready(function () {
$('.linklist a').each(function () {
alert(this.text);
});
});
Upvotes: -1
Reputation: 30666
this
inside the event handler is the native DOM element.
When you do this.id
you are getting the id
property of the DOMElement (an anchor). But it does not have a text
property.
To get the text of the anchor, use:
// turn "this" to a jquery object and use .text()
$(this).text()
Or staying native javascript, you can:
this.textContent || this.innerText
Upvotes: 3