Expert wanna be
Expert wanna be

Reputation: 10624

Jquery, How to get text in span tag using foreach

<span class='url_link'>1</span>
<span class='url_link'>2</span>
<span class='url_link'>3</span>
<span class='url_link'>4</span>
<span class='url_link'>5</span>

$(".url_link").each(function(idx, obj){
        console.log(this);  // <span class="url_link">
    console.log(this.text()); // TypeError: this.text is not a function
});

How can I grep the text in span tag?

Upvotes: 0

Views: 579

Answers (2)

Sushanth --
Sushanth --

Reputation: 55750

console.log(this.text())

Should be

console.log($(this).text())

.text() is a jQuery method and this is a DOM Object ..

So need to convert that into a jQuery Object before you use .text() on it..

If you want to use the native javascript then you can try this

console.log(this.innerHTML)

Check Fiddle

Upvotes: 0

j08691
j08691

Reputation: 207901

Use console.log($(this).text());

You were trying to use a jQuery function on a non-jQuery object.

jsFiddle example

Upvotes: 2

Related Questions