Adrian
Adrian

Reputation: 3062

How to remove character from every class in jQuery?

I have an Angular 4 app that includes a "/" for every .inner-page class in a html element.

For example:

<div _ngcontent-c0="" class="inner-page /login">
<div _ngcontent-c0="" class="inner-page /register">

I want to remove the "/" character so it should look like:

<div _ngcontent-c0="" class="inner-page login">
<div _ngcontent-c0="" class="inner-page register">

Any help is appreciated. Thanks.

Upvotes: 0

Views: 191

Answers (2)

UncaughtTypeError
UncaughtTypeError

Reputation: 8752

Split (.split()) the class attribute (.attr()) values by the character in question then rejoin (.join()) them.

Code Snippet Demonstration:

$('.inner-page').each(function(){
  var elClass = $(this).attr('class').split(' /').join(' ');
  console.log(elClass);
  $(this).attr('class', elClass);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div _ngcontent-c0="" class="inner-page /login">Login</div>
<div _ngcontent-c0="" class="inner-page /register">Register</div>

Reference:

  1. .split() Ref - Javascript MDN
  2. .attr() Ref - jQuery API
  3. .join() Ref - Javascript MDN

Upvotes: 2

oreopot
oreopot

Reputation: 3450

Try the following code :

 var value;
 $(document).ready(function(){
    $(".inner-page").each(function() {
        value = $(this).attr("class").replace("/", "");
        $(this).attr("class", value);
    });
 });

Upvotes: 1

Related Questions