Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the classes that begin with a certain string? [duplicate]

I am trying to remove the classes that begin with a certain string.

Here is my code:

function myfunction() {
  const body = document.getElementsByTagName('div')[0];
  body.classList.remove('page*'); //remove page-parent
}

myfunction();
<div class="page-parent pop">
  check out
</div>
like image 915
3gwebtrain Avatar asked Jun 26 '18 07:06

3gwebtrain


People also ask

How do I remove a repeating element from a string?

We can remove the duplicate characters from a string by using the simple for loop, sorting, hashing, and IndexOf() method.

How to remove every class starting with'El'from an element?

"Every class starting with 'el' is removed from the element."; Select a particular element. Use .className property to get access to all the classes. Use .split () method to get all classes as a element. Use .filter () function to filter out the classes which are not starting with certain character. Finally, put those classes with the element.

How do I remove all classes that start with a certain name?

What you need to do is parse all of the class names assigned to each element, then remove the ones that meet the correct criteria. That's what rminde's solution does. The reason it is so complicated is because of your "starts with" criteria. Alabama M.. Re: How do I remove all classes that start with a certain name?

How to filter out classes that do not start with certain characters?

Use .split () method to get all classes as a element. Use .filter () function to filter out the classes which are not starting with certain character. Finally, put those classes with the element. Example 2: This example using the approach discussed above. begin with a certain string JavaScript?

How to remove classes from an element in JavaScript?

Here are a few of the most used techniques discussed. We are going to use JavaScript. Select a particular element. Use .className property to get access to all the classes. Use .replace () method to replace all the specific classes by space (Which means classes are removed from the element).


3 Answers

OK , there are many solution to this

first what causes your problem ?

in your code you just remove the class that should be page* and that's not a regular expression because you're using the double quotes .

also classList is a DOMTokenList which is a collection , so we need to iterate over it to test each value in the list to see if it meets specific criteria

how to solve this problem ?

first solution

use startWith string method

how ?

  1. iterate over each class in classList check if it starts with what you search for remove it if yes

example

const p = document.getElementById('p');
p.classList.forEach(item=>{
    if(item.startsWith('page')) {
        p.classList.remove(item) ;
    }
})
.page-blue {
    color: blue;
}
.c {
    text-align: center;
    font-size: 1.4rem;
}
<p id="p" class=" c page-blue">
    hello I am p
</p>

also you can use simple regular expression for that

how ? using special-caret that tests if the input starts with the specified value

example

const p = document.getElementById('p');
p.classList.forEach(item=>{
    if(/^page/.test(item)) {
        p.classList.remove(item) ;
    }
})
.page-blue {
    color: blue;
}
.c {
    text-align: center;
    font-size: 1.4rem;
}
<p id="p" class=" c page-blue">
    hello I am p
</p>
like image 137
mostafa tourad Avatar answered Nov 06 '22 03:11

mostafa tourad


This is a simple example by using loop regular expression check and remove item which is matched.

function myfunction() {
  var div = document.getElementsByTagName('div')[0];
  
  var matches = [];
  div.classList.forEach(function (value) {
    //remove page-parent
    if (/^page.+/.test(value)) {
      matches.push(value);
    }
  });
  matches.forEach(function (value) {
      div.classList.remove(value);
  });
}

myfunction();
<div class="page-parent pop">
  check out
</div>
like image 24
Terry Wei Avatar answered Nov 06 '22 04:11

Terry Wei


Here's a very "ES6" way of doing it.

We're converting the classList into an array with a spread operator then we are filtering the array with regex. This leaves us with an array of classes we want to remove so we can do a forEach over them and remove them.

I've used a little CSS to display the divs classes next to it.

const body = document.querySelector('div');

[...body.classList].filter(c => {
  return c.match(/^page.*/)
}).forEach(e => {
  body.classList.remove(e)
});
div:after {
  content: "("attr(class)")";
}
<div class="page-parent pop">
  check out
</div>

I hope you find this helpful.

like image 44
Andrew Bone Avatar answered Nov 06 '22 02:11

Andrew Bone