Crudler
Crudler

Reputation: 2286

jquery selects returning undefined value - asp.net webform

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

Answers (2)

tarmaq
tarmaq

Reputation: 422

$(document).ready(function () {
    $('.linklist a').each(function () {
        alert(this.text);
    });
});

Upvotes: -1

Didier Ghys
Didier Ghys

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

Related Questions