Getting closest input value from clicked element with jQuery
$(".add").on("click", function () { var val = $(this).closest("div.options").find("input[name='quantity']").val();});
The strategy here is:
- Use
closest
to find the closest ancestor matching the given selector (in this case adiv
with classoption
) - Then use
find
to find theinput
with the givenname
using the attribute equals selector. - Finally, use
val
to retrieve the value of theinput
.
You need to use closest
to find the proper enclosing element and then work from there.
$(link).closest(".options").find("input[name=quantity]")
$(this).parent().parent().find("input[name=quantity]").val()
This should do the trick