newbie
newbie

Reputation: 1980

Selecting dynamically generated element in jQuery

I have this code:

<c:forEach items="${ sample }" var="test">
    <tr>
        <td><strong>${ test.test }.</strong></td> // I want to get the text
        <td>
            <fieldset class="span11">
                <div class="control-group">
                    <label class="control-label"><strong>${ test.blah }</strong></label>
                    <div class="controls" id="question${ test.blah }">
                        <c:forEach var="beng" items="${ anotherSample }">
                            <form:radiobutton path="boom" class="question-choices" data-label="${ choices }" value="${ beng-boom }"/><br>
                        </c:forEach>
                    </div>
                </div>
            </fieldset>                 
        </td>
    </tr>
</c:forEach>

I want to get the text from the first <td> from the radio button:

What I tried so far is:

$('.question-choices').on('change', function() {
    console.log( $(this).parent('tr').closest('td').text() );
});

But this returns empty string

Upvotes: 1

Views: 1067

Answers (2)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

try this;

$('.question-choices').on('change', function() {
    console.log( $(this).closest('tr').find('td:first').find("strong").html() );
});

Upvotes: 2

qwertynl
qwertynl

Reputation: 3933

parent() only gets a direct parent.

Try using .parents:

$('.question-choices').on('change', function() {
    console.log( $(this).parents('tr').closest('td').text() );
});

Upvotes: 1

Related Questions