1

I am trying to iterate through all the span elements that is the children of a div:

 $('#recommendTextArea').children('span').each(function () {
            console.log(this.html()); 
        });

However I always get this error:

Uncaught TypeError: Object #<HTMLSpanElement> has no method 'html' 

I tried changing it to text(), but it also doesn't work

adit
  • 32,574
  • 72
  • 229
  • 373
  • 2
    could also worth a read: [jquery-this-vs-this](http://stackoverflow.com/questions/1051782/jquery-this-vs-this) – Paolo May 21 '13 at 09:05

4 Answers4

3

Try with this :

console.log($(this).html()); 

You were trying to call .html() on DOM element, not a jQuery object.

sohel khalifa
  • 5,602
  • 3
  • 34
  • 46
0
   $('#recommendTextArea').children('span').each(function () {
        console.log($(this).text()); 
    });
sachin
  • 13,605
  • 14
  • 42
  • 55
0

1) Try $(this).html() or $(this).text()

2) you could use alternatively $('#recommendTextArea').find("span") to get the spans

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
0

.html() is a jQuery function and must be called on a jQuery object. You can create a jQuery object from this by passing it to the jQuery object, like $(this)

In your code this is a <span> which does not have any native method called .html(), hence the JavaScript error.

andyb
  • 43,435
  • 12
  • 121
  • 150