0
<tr role="row" class="odd parent">
    <td class="quantity"><input type="text"></td>
</tr>

<tr class="child">
    <td class="child" colspan="8">
        <li data-dtr-offset-index-"7">
            <span class="dtr-title"></span>
            <span class="dtr-data">
                <button type="button" class="btn btn-primary">Add to Cart</button>
            </span>
        </li>
    </td>
</tr>   

The parent row has an input for quantity. The child row has a button. I want to click the button and return the quantity. So far I've tried:

$(".btn-primary").click(function(e) {

    var qty = $(this).closest("input").val();

});

But this returns undefined.

Can someone help?

Thanks in advance!!

2
  • 1
    where is input tag in your html? Commented Mar 20, 2016 at 16:26
  • sorry, i made an edit and didn't space the sample code properly. just updated it! @DmitriyLoskutov Commented Mar 20, 2016 at 16:32

1 Answer 1

1

The parent row has an input for quantity. The child row has a button.

Note, the first tr is not a parent of second tr; the tr elements are siblings within html at Question.

Try adjusting selector at .closest() to tr , chain .prev() with selector tr , .find() with selector input , .val()

$(this).closest("tr").prev("tr").find("input").val()
Sign up to request clarification or add additional context in comments.

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.