0

I'm looping through several instances of HTML that looks like this:

<div class="matchup-container">
    <div class="gamequestion">
        <strong>NBA: Who will WIN this matchup?</strong>
    </div>
    <table class="mg-gametableQ">
        <tbody>
            <tr>
                <td class="mg-column1 start">
                    <div class="matchupDate">
                        <span class="startTime">11:00 AM</span>
                    </div>
                </td>
                <td class="mg-column3 opponents  ">
                    <span>
                        <strong>
                            <a>Team</a>: Win
                        </strong>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="mg-column1 start">
                    <div class="matchupDate">
                        <span class="startTime">11:00 AM</span>
                    </div>
                </td>
                <td class="mg-column3 opponents  ">
                    <span>
                        <strong>
                            <a>Team</a>: Win
                        </strong>
                    </span>
                </td>
            </tr>
        </tbody>
    </table>
</div>

There are several matchup-container divs to loop through, so I'm using .each() to do that. Then I want to loop through each mg-column3 inside that div, so I use .each() again. Finally, I want to pull the text inside the <a> (in this case, "Team"), and append that inside the mg-column3 div. My jQuery code is here:

$('.matchup-container').each(function() {
    var title = $.trim($(this).find('.gamequestion').text());

    $(this).find('.mg-column3').each(function () {
        if (isStraightUp(title))
        {
            var teams = getStraightUpTeams(this);
            $(this).append('<span class=odds-favorite>' + teams[0] + '</span>');
        }
    });
});

function isStraightUp (title)
{
    return true;
}

function getStraightUpTeams(matchup)
{
    var teams = [];

    teams[0] = $(matchup).find("a")[0].text();
    teams[1] = $(matchup).find("a")[1].text();

    return teams;
}

But nothing happens. The Chrome dev tools tell me that I can't call .text() on an undefined element, so everything crashes. But I can't figure out why the jQuery isn't grabbing the text inside the <a> tags that I want. Here's a JSFiddle. Can anyone help?

2
  • Your mg-column3 elements only contain a single a each. Why are you trying to access 2? The 2nd doesn't exist. Commented Mar 18, 2014 at 3:18
  • @JamesMontagne just realized that myself, and fixed it. thanks Commented Mar 18, 2014 at 3:21

3 Answers 3

1

Since $(matchup) return a jQuery object, you need to use jQuery method like .eq() or :eq() selector instead:

teams[0] = $(matchup).find("a").eq(0).text();
teams[1] = $(matchup).find("a").eq(1).text();

Updated Fiddle

or:

teams[0] = $(matchup).find("a:eq(0)").text();
teams[1] = $(matchup).find("a:eq(1)").text();

Updated Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, this works! i'll accept this answer when i can because you were first.
use .eq() over :eq() every single time. way, way faster.
@PlantTheIdea I've added the demo with .eq() :)
1

Use .eq() in Jquery.

function getStraightUpTeams(matchup)
{
    var teams = [];

    teams[0] = $(matchup).find("a").eq(0).text();
    teams[1] = $(matchup).find("a").eq(1).text();

    return teams;
}

Comments

1

You have an error in getStraightUpTeams. Look at your console to see the error

Uncaught TypeError: Property 'text' of object [object HTMLAnchorElement] is not a function

You are trying to call a method text in a dom element reference which does not have such a method, you need to use jQuery wrapper to call the method .text()

function getStraightUpTeams(matchup) {
    var teams = [],
        $as = $(matchup).find("a");

    teams[0] = $as.eq(0).text();
    teams[1] = $as.eq(1).text();

    return teams;
}

Demo: Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.