var definitionTerms = document.querySelectorAll('dt') // Add 'id' attribute to accordion titles that do not have a manually placed anchor $(definitionTerms).each(function () { if ($(this).attr('id')) { // Do nothing since the title already has 'id' attribute } else { // Add 'id' attribute with value set to title text var titleContent = $(this).text() titleContent = titleContent.replace(/ /g,'-') titleContent = titleContent.replace(/[^a-zA-Z0-9_-]/g,'') $(this).attr('id', titleContent.toLowerCase()) } });