2

When i add or remove a class with javascript then reload the page ..

it wont work

I tried this code, and put it in on the header

<script>
    $(document).ready(function() {
  $('.boxAdRight').removeClass('boxAdRight').addClass('active');
});
</script>

and this is my HTML I want to remove the class boxAdRight and change it with .active class

<div class="panel-body">
   <div class="tab-content">
      <div class="tab-pane fade in active" id="data">
         <div class="getData">
            <div class="row">
               <div class="col-md-12 col-sm-12 col-xs-12">
                  <div class="servers">
                     <ul>
                        <li class="active" 
                           id="s_0"onclick="getServer(this.id,0);"> 
                           <i class="fa fa-video-camera"></i> server1
                        </li>
                     </ul>
                  </div>
               </div>
               <div class="col-md-3 col-sm-4 col-xs-12 fRight">
                  <div class="episodes boxAdRight">
                     <h2>episodes</h2>
                     <ul>
                        <li>
                           <a href="http://localhost/wordpress/newarticle13/">                                  episode 1</a>
                        </li>
                        <li class="active"><a href="http://localhost/wordpress/newarticle13/"> episode 2</a>
                        </li>
                     </ul>
                  </div>
               </div>
               <div class="col-md-9 col-sm-8 col-xs-12 fRight">
                  <div class="boxVideos">
                     <div class="getCode"><iframe src="https://*******" scrolling="no" frameborder="0" width="700" height="430" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</div>

thank you

7
  • 2
    what do you mean by "it won't work?" have you checked your browser console on error messages? Commented Sep 17, 2018 at 13:34
  • 1
    thank you for your reply, yes i checked my browser, no errors ! Commented Sep 17, 2018 at 13:40
  • 2
    you change the class with javascript and that's ok. Then you say you reload the page. Are you saying that you expect the change to be persistent on page reload? Commented Sep 17, 2018 at 13:53
  • 1
    @LelioFaieta That reloading page part caught my eye too, however the code should execute the same way every time the page is loaded, so I'm still confused as to what the OP's issue is. Unclear. Commented Sep 17, 2018 at 14:03
  • 1
    I'm working on wordpress, I can see the code of javascript on source code of the page, but nothing change Commented Sep 17, 2018 at 14:17

5 Answers 5

4

As a first observation I would like to mention the fact that JQuery's removeClass function returns one or more space separated class names that should be removed. As a consequence, you should not apply addClass over the result of removeClass.

There might be other issues also in your code though...

Sign up to request clarification or add additional context in comments.

Comments

1

Replace your .boxAdRight class with .episodes class and it'll work as you want.

  <script>
        $(document).ready(function() {
            $(".episodes").removeClass("boxAdRight").addClass("active");
       });
    </script>

2 Comments

Works fine with it
Have you removed "episodes" class from " <div class="episodes boxAdRight">" this line? OR Check in private browser or CTRL+F5. may be it's cache issue.
0

I believe that the order of the method calls should be

$('.boxAdRight').addClass('active').removeClass('boxAdRight');    

In the original example $('.boxAdRight').removeClass('boxAdRight').addClass('active'); the selector .boxAdRight is being used, but then that class is being removed with .removeClass('boxAdRight'), so .addClass('active') is being called on a selector with no matching elements.

1 Comment

No, this is not correct. It will work fine. Testing a simple example demonstrates this. jsfiddle.net/j08691/2aqt7uow
0

get it like that will solve it

$('.episodes').addClass('active').removeClass('boxAdRight');

in normal case yours too must work ?

2 Comments

I tried on jsfiddle.net/j08691/2aqt7uow and its working well ! if you wanna have active class. open the console with f 12 and you will see active is added to your div
yes i see, it works for you, but in my case, I'm working on wordpress, and I added the code in <head> section of header.php
-1

You can also simplify this code by replacing of class instead of removing it.

$('.boxAdRight').className = 'active';

how to replace class HTML name with javascript

1 Comment

thank you, I tried that code too, but tha class still

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.