1

I am trying to remove some string from a string in Jquery but it doesnt work and my variable still looks the same.

var classes = $("body").attr('class');
alert('.side-nav' + classes);
classes = classes.replace('.side-navblog parent-  page- how-it-works-','');
$(".side-nav" + classes).css("display","block");

When I alert classes after it has been replaces, there is still .side-navblog parent- page- how-it-works- in the string which I do not want.

4
  • please share the alerted value Commented Jan 23, 2014 at 2:54
  • 3
    Why not use .removeClass? Commented Jan 23, 2014 at 2:54
  • also the class attribute value may not have . so '.side-navblog parent- page- how-it-works-' should be 'side-navblog parent- page- how-it-works-' Commented Jan 23, 2014 at 2:55
  • The first alerted value is .side-navblog parent- page- how-it-works-applications The alerted value that I want is applications. Commented Jan 23, 2014 at 2:55

3 Answers 3

1
classes = classes.replace('.side-navblog parent- page- how-it-works-','');
                                                      ^^ double space here 

or try a regex

classes = classes.match(/how-it-works-([^$\s]*)/)[1]
Sign up to request clarification or add additional context in comments.

Comments

1

Try regular expression,

classes = classes.replace(/.side-navblog|parent-|page-|how-it-works-/g, '');

Comments

1

What I can figure out from your comment is you have put to spaces just before 'page-'. Other than it should work fine.

classes = classes.replace('blog parent- page- how-it-works-','');

Check out here - http://jsfiddle.net/Fb367/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.