- Home
- Forum
- ASP.NET Core - EJ 2
- Handle click on ListBox element
Handle click on ListBox element
Hello!
I'm trying to implement logic that would allow some action execution (loading page section using AJAX) based on click on the ListBox item.
My ListBox looks like this:
<div class="control-section">
<div id="listbox-selection">
<h4>Active scenarios:</h4>
<ejs-listbox id="scenario-listbox" dataSource="@Model.ScenarioList" actionComplete="onComplete" >
</ejs-listbox>
</div>
</div>
So it loads elements from data source. ScenarioList looks like
ScenarioList = new List<object>();
ScenarioList.Add(new { text = "Email", id = "email" });
So if one list item should have id == "email". So if I would do:
@section Scripts {
<script>
function onComplete() {
alert('1');
}
document.getElementById('email').addEventListener('click', () => {
alert('2');
});
</script>
}
Listener won't be added and '2' will not be displayed on 'email' element click. As far as I understand, this is because items are generated on-fly. And if I move ' document.getElementById...' to onComplete - it won't work as way.
I have not found instructions about handling item click in the documentation. Could someone, please, advise, what is the proper way of handling a left button click on a particular list element?
Thanks in advance!
|
<ejs-listbox id="listbox" dataSource="@ViewBag.DefaultData" change="change"></ejs-listbox>
<script>
function change(args) {
if (args.items[0].id == 'email') {
alert('email');
}
}
</script> |
Thanks a lot!
Works great!
- 3 Replies
- 3 Participants
-
AL Alex
- Aug 5, 2021 11:31 AM UTC
- Aug 11, 2021 05:45 AM UTC