Robel Haile Robel Haile - 1 year ago 67
HTML Question

the "closest()" method in jquery wont work

so i have this html code and jquery code. And when i press a tablerows "produkter" button whith the class name "fa-products", i want to find the hidden field input that is on the same tablerow as the button you choose to click(every tablerow have a hidden field input and a "produkter button"). Then i want to save the value of the hidden field in a variable thats all can anyone help me? when i "console.log(discountId);" it responds undefiend

<div class="eastSide row top-buffer col-xs-8" style="overflow:scroll; height:250px;">

<table style="width:100%">
@foreach (var discount in Model.DiscountList)


<td><input name="codeTextBox" id="codeTextBox" value="@discount.Code" maxlength="18" /></td>

<td><input type="checkbox" id="activeCheckBox" name="activeCheckBox" checked="@discount.Active" /></td>
<td><input type="datetime" value="@discount.CreatedDate" readonly /></td>
<input type="button" value="Radera" class="fa fa-remove" data-url="@Url.Action("DeleteDiscountCode","Discount",new { id= discount.Id})" />
<input type="button" value="Uppdatera" class="fa fa-update" data-url="@Url.Action("UpdateDiscount","Discount")" />
<input type="button" value="Produkter" class="fa fa-products" id="@discount.Id" data-url="@Url.Action("chooseProductsForDiscountCode","Discount")" />
<td><input id="id" type="hidden" value="@discount.Id" /></td>


$(".fa-products").on("click", function (e) {

var discountId = $('input[type="hidden"]').val();


Answer Source

It will not work, because the hidden input is not the parent of the registered element.

Probably this will solve your issue: $('tr').find('input[type="hidden"]').val();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download