I have a div with a class which I need to switch out based on what the user is interacting with.
<div class="book-chapter-name chapter2" data-lhg="chapter-info">
<span>Chapter String Here</span>
</div>
I have the current div in a jQuery object and this has been found using:
$('div[class*="chapter"]')
What I want to do is remove the existing class and insert a new class so that chapter2
becomes chapter3
var chapterString = $('#page'+page).attr('data-lhg-chapter'),
chapterNumber = $('#page'+page).attr('data-lhg-chapter-number'),
chapterInfo = $('[data-lhg="chapter-info"]');
if($('div[class*="chapter"]')){
$('div[class*="chapter"]').removeClass('chapter[0-9]');
}
$(chapterInfo).text(chapterString).addClass('chapter'+chapterNumber);
This doesn't seem to work, I think it is to do with the string passed to removeClass
but I am not sure what to put in there to get the right result.