Replace part of class name
I'm trying to change part of class names. I'm trying to change the text in red so it has the same number as the div's class (ie all should be 3_page0). Here's the html:
- <div class="lslide_wrap_stacks_in_3_page0">
- <a href=... class="lslide_stacks_in_3_page0 ls-w1_stacks_in_4_page0"...>
- <span class="ls-wrapper_stacks_in_4_page0"...>
- ... more similar spans
- </a>
- </div>
Here's my script:
- $('div[class^="lslide_wrap"]').each(function(i) {
- var clcheck = $(this).attr('class').substr($(this).attr('class').indexOf("in_") + 3);
- console.log('Wrap Class Check: ' + clcheck); // "3_page0"
- var acheck = $(this).children('a').attr('class').substr($(this).children('a').attr('class').lastIndexOf("in_") + 3);
- console.log('Ahref Check: ' + acheck); // "4_page0"
- if (clcheck != acheck) { // if they don't match, then change the class text
- var lscheck = $(this).children('a span[class^="ls-wrapper"]').attr('class').substr($(this).children('a span[class^="ls-wrapper"]').attr('class').indexOf("in_") + 3);
- console.log('ls Check: ' + lscheck);
- var aclassinit = $(this).children('a').attr('class');
- var lsclassinit = $(this).children('a span[class^="ls-wrapper"]').attr('class');
- console.log('ls Class: ' + lsclassinit);
- var anew = aclassinit.replace(acheck, clcheck);
- var lsnew = lsclassinit.replace(lscheck, clcheck);
- console.log('Ahref New: ' + anew);
- console.log('ls New: ' + lsnew);
- }
- $(this).children('a').attr('class', anew);
- $(this).children('a span[class^="ls-wrapper"]').attr('class', lsnew);
- });
Line #5 was working so then tried to do similar w/ line #8, but getting undefined error. Not sure why it's not working - expect it to return "4_page0" Thanks.