Here is my code. I do not know what I am missing. I went to the list.js documentation and I followed every step. I even went to jfiddle and did an example and it worked. I think I am doing something wrong maybe because I am using ejs. Can someone help me out. Below is my HTML code and Javascript.
Thanks
<div class="client-names">
<!-- class="search" automagically makes an input a search field. -->
<input class="search" placeholder="Search" />
<!-- class="sort" automagically makes an element a sort buttons. The date-sort value decides what to sort by. -->
<button class="sort" data-sort="fname">
Sort By First Name
</button>
<button class="sort" data-sort="lname">
Sort by Last Name
</button>
<ul class="list">
<% clients.forEach(function(client) { %>
<li>
<span class="lname"><%= client.lname %></span>
<span class="fname"><%= client.fname %></span>
<br>
<form class="delete-form" action="/clients/<%= client._id %>?_method=DELETE" method="POST">
<button class="btn btn-danger btn-md" data-id="<%= client._id %>" id="delete-client">Delete Client</button>
</form>
<a href="/clients/<%= client._id %>" data-id="<%= client._id %>" class="btn btn-primary btn-md view-client-button">View Client</a>
</li>
<% }); %>
</ul>
</div>
JAVASCRIPT
<script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
<script src="/js/main.js"></script>
<script type="text/javascript">
var options = {
valueNames: ['lname', 'fname']
};
var clientList = new List('client-names', options);
</script>
list.js requires an element as first parameter at init. From doc:
So, if you change this line:
to:
your code works.
Instead, if you need to use the class name, you need to get the element by class like in:
HOW TO SORT...
It's enough to add the click event handler to your sort buttons and call the sort method:
or in javascript: