learning
learning

Reputation: 11745

reading span text

I have a div in which I have spans and I want to read all the text of the inner span. I am not able to read the span "name" text. What is wrong with the following piece of code?

<div id= "myList">
<span class="FirstSpan">
<span class = "name">

</span>

</span>

</div>

 var values= [];
        $('#myList').find('span.name').each(function () {
            $alert($(this).html());
            values.push($(this).html());
        });

Upvotes: 2

Views: 475

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

$alert() should be alert()

you can try

$(this).text()

Full code

var values = [];
$('#myList span.name').each(function () {
     alert($(this).text());
     values.push($(this).text());
});

DEMO

Other way:

var values = [];
$('#myList span.name').text(function(i, text) {
    alert(text);
    values.push(text);
});

DEMO

Related refs:

Upvotes: 3

VisioN
VisioN

Reputation: 145478

There is no function $alert() and that is the problem. Use alert() instead.

However, in your case I'd better use map method:

var values = $("#myList span.name").map(function() {
    return $.trim(this.innerHTML);
}).get();

DEMO: http://jsfiddle.net/m5QsA/

Upvotes: 1

Related Questions