I have a view that shows a list of classes sorted by category. The category names all start with 'MAL - '. I have tried using jQuery to delete the text, but nothing happens. Where am I going wrong?
HTML:
<div class="aof_kurskategori">
<div class="aof_kurskategoriTittelCelle">[wpv-taxonomy-title]</div>
<div class="aof_kurskategoriCelle">[wpv-view name="kursliste-02"]</div>
</div>
Jquery:
// Function to remove 'MAL - ' from the beginning of a text string.
function filtrerTekst(text) {
const malPrefix = "MAL - ";
if (text.startsWith(malPrefix)) {
return text.slice(malPrefix.length);
}
return text;
}
$(document).ready(function() {
const element = $('.aof_kurskategoriTittelCelle');
const tekst = element.text();
const filtrertTekst = filtrerTekst(tekst);
element.text(filtrertTekst);
});
Hi there,
It seems like your jQuery code is almost correct, but it's only targeting the first element with the class .aof_kurskategoriTittelCelle. If you have multiple elements with this class and you want to remove the prefix 'MAL - ' from all of them, you need to iterate through each element.
Here's the modified jQuery code that will work for multiple elements:
// Function to remove 'MAL - ' from the beginning of a text string.
function filtrerTekst(text) {
const malPrefix = "MAL - ";
if (text.startsWith(malPrefix)) {
return text.slice(malPrefix.length);
}
return text;
}
$(document).ready(function() {
$('.aof_kurskategoriTittelCelle').each(function() {
const element = $(this);
const tekst = element.text();
const filtrertTekst = filtrerTekst(tekst);
element.text(filtrertTekst);
});
});
Make sure that this script is loaded after the HTML content, or use a $(window).on('load', function() {...} if you need to wait for all content to be loaded (including the content loaded by the Toolset Views plugin). Also, make sure jQuery is loaded on the page for this script to work.
I suggested the code above without having access to the actual URL that generates the code. So if it is not working I'd appreciate it if you can give me the means to have access to the URL in question on my browser to give you a better idea on what to do.
Just to make sure we are on the same page, this is not a Toolset specific question and it is rather a general jQuery issue which is outside of our support scope, so we will not be able to give you an exact working code, rather suggestion on what to do to get to the answer.
Thanks.