Is it possible to set an action listener for a list item in HTML and pass what the user clicked in JavaScript?
Index.html:
<div class="dropdown">
<button class="btn d-flex align-items-center" type="button">
<h3 class="fs-1 fw-bold">Set Limit</h3>
</button>
<ul class="dropdown-menu">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
<script>
$(function() {
// I want this to be able to get its value
// depending on what the user clicked from the <li> tag
var limit = 1;
});
</script>
I updated my code now to
Index.html:
<select id="dropdown">
<option value="A" data-number="1">option1</option>
<option value="B" data-number="2">option2</option>
<option value="C" data-number="3">option3</option>
<option value="D" data-number="4">option4</option>
</select>
<script>
$('#dropdown').change(function(){
var number = $(this).find('option:selected').attr('data-number');
});
$(function() {
/* I am unsure now on how to get the number variable from above and put it here in my var limit */
var limit = ;
});
</script>
I guess it really depends on your flow of data. Here I'm grabbing the selected value from the
selectelement within thechangehandler, and passing it as an argument to a function calledmainwhich logs it to the console.Note: I moved the value from the
data-numberattribute to the value (otherwise what's the point of the value...?)