I have a table, in which I have hundreds of rows, and in which each row looks something like this (with different information for each row, of course)
<tr>
<input class="prodId" hidden value="<?php echo $product['id']; ?>">
<td class="thead"><?php echo $product['prod_name']; ?></td>
<td class="thead"><?php echo $product['prod_price']; ?></td>
<td class="thead"><?php echo $product['prod_qty']; ?></td>
<td class="thead">
<select class="availability-options">
</select>
</td>
</tr>
Everything is working fine in the option change event of the select
. What I am trying to do is get the value of the id that is in the input
. i'm trying this
$(".availability-options").on("change", function(){
let optionSelected = $(this).val();
let _id = $(this).closest("input .prodId").val();
....
And also try this
let _id = $(this).parent().parent().children(".prodId").val();
I understand that both selectors return a Jquery type object, but I can't figure out how to get the one value
from the input
. It always returns an undefined value. How do I get the value of input
?
In the
input
: you declare it ashidden
, the correct thing istype="hidden"
. I tried it as you have it and it marked me undefined as you say.On the other hand,
closest
it goes up the DOM hierarchy to find the indicated element (by looking for its ancestor). In this case, you will never find theinput
as your ancestor. Therefore, we ascend totr
and there we look for theinput
as a child:Try as follows:
I leave you the Jquery documentation for this case http://api.jquery.com/val/
Try removing the space in the selector
$(this).closest("input.prodId")
Also, it seems to me that the
input
won't render where they should if you add them as children of atr
. I would move them to thetd
sameselect
The closest does not work because the input is not a child of the select example: https://www.w3schools.com/jquery/traversing_closest.asp
Try to use via a function