Getting closest input value from clicked element with jQuery Getting closest input value from clicked element with jQuery jquery jquery

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 a div with class option)
  • Then use find to find the input with the given name using the attribute equals selector.
  • Finally, use val to retrieve the value of the input.


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