I have this small HTML page which exposes my problem. I want to get the id of the first ancestor of my input element (#input-1). When the user changes the option value, the JavaScript code should get the id of the #li-1 element and display it. Unfortunately all I get is a undefined and I don't know why.
The parents() function should traverse up the DOM tree and find the li element, then get the id via the .attr() function
$("#input-1").on("change", function(elem) {
var id = $(elem).parents("li").attr("id");
alert(id);
})
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<li id="li-1">
<div class="item-content item-input">
<div class="item-inner">
<div class="item-title item-label">Test</div>
<div class="item-input-wrap">
<select id="input-1">
<option value="0">test0</option>
<option value="1">test1</option>
</select>
</div>
</div>
</div>
</li>
</body>
</html>