I have the following scenario:
I have a table that is populated with user data like name, surname, location and courses in a structure like the following:
<tr>
<td data-label="First Name" class="wppb-name"><span>Mutanga</span></td>
<td data-label="Last Name" class="wppb-name"><span>Eto Emule</span></td>
<td data-label="Location" class="wppb-name"><span>Somewhere</span></td>
<td data-label="Courses" class="wppb-name seminaria-multiselect">Course 1, Course 2, Course 3, Course 4, Course 5, Course 6</td>
</tr>
The courses are fetched in a long line of text with no separation which i dont want neither i can insert span elements manually to each course. I change it with the following jQuery snippet. This separates the courses and insert them into spans and it works.
$('.seminaria-multiselect').each(function() {
$(this).html($(this).html().split(', ').map(function(el) {
return '<span>' + el + '</span>';
}));
});
Now the problem is that when i apply filters (for example, filter the users for specific locations or specificcourses), it does the filtering using AJAX and it loses the 'span formatting' of the above snippet.
I understand that this can be solved using the on() method, something like this:
$(document).on('change', '.seminaria-multiselect', function() {
$('.seminaria-multiselect').each(function() {
$(this).html($(this).html().split(', ').map(function(el) {
return '<span>' + el + '</span>';
}));
});
});
This snippet however does not work properly all by itself. When the page is loaded, the text is not separated into spans neither after applying filters.
If i keep both the snippets like so:
$('.seminaria-multiselect').each(function() {
$(this).html($(this).html().split(', ').map(function(el) {
return '<span>' + el + '</span>';
}));
});
$(document).on('change', '.seminaria-multiselect', function() {
$('.seminaria-multiselect').each(function() {
$(this).html($(this).html().split(', ').map(function(el) {
return '<span>' + el + '</span>';
}));
});
});
then it applies the span separation on page load but filtering still doesn't produce the result i want.
Q1) What should i correct in order to make it work? Q2) Is there a more efficient way to do this? Is it necessary to keep both snippets (the first one when the page loads and 2nd one for when it filters)
I thank you very much for your time :)