Eoin
Eoin

Reputation: 357

cant get text from an element in jQuery

I have a HTML setup as follows and am trying to use jQuery to access the textin the font field. The table has multiple rows, hence the first line in the jQuery, for some reason I'm getting an empty string returned for the title variable. Thank you for your help!

HTML

<table class="table">
    <tbody>
        <tr>
            <td class="head">
                <a href="link" target="_self">
                    <p>
                        <font>SomeText</font>
                    </p>
                </a>
            </td>
        </tr>
    </tbody>
</table>

jQuery

$('.table').each(function(){
    $(this).filter(function(){
        var data   = $(this);
        title = data.children(".head").text();
        json.array.push({
            "title" : title
            });
    })
})

Upvotes: 0

Views: 345

Answers (2)

Stryner
Stryner

Reputation: 7318

.head is not a child of .table. So .children(".head") will not return any elements. You should use .find instead.

Also, the .filter seems unnecessary:

$('.table').each(function(){
    var data = $(this);
    title = data.find(".head").text();
    json.array.push({
        "title" : title
    });
})

Upvotes: 1

mpallansch
mpallansch

Reputation: 1194

Your element with class head has no text in it, you need to adjust your selector to get the actual font element, like this:

data.children(".head font").text();

Upvotes: 0

Related Questions