I am removing class and setting another, but it is not happening?

57 views Asked by At

I want Ads through prescribed class removal Get ads div set up.

<div class='insideAd'><p>Ad</p></div>

$(function() {
  $("body").ready( function() {
    // Ads are loaded
    $(".loadAd").removeClass();
    $("insideAd").addClass();
  });
});
2

There are 2 answers

0
Homungus On

you may want to do something like this: first remove all classes and then add new class to matching elements:

$(function() {
    // Removes all classes and after it adds class 'insideAd' 
    $(".loadAd").removeClass().addClass("insideAd");
});
1
Code-Apprentice On

To select an element with a class that already exists on it:

$(".loadAd")

When you do $("insideAd") you are trying to select a tag with that name, which doesn't exist. Even if you add the . with $(".insideAd"), you won't select any elements because none exist with this class name. To use this syntax the class must already exist.

To remove a class from that selected element, you should state which class to remove. Otherwise it will remove all classes from that element.

$(".loadAd").removeClass("loadAd")

Finally to add another class, you also have to pass the class to add:

$(".loadAd").removeClass("loadAd").addClass("insideAd")