JQuery .addclass() after page load

17,286

jQuery is scripting language ... When loading the page it will definitely remove your previous functionality as click event doesn't fired so it won't work....

After loading the page just put some logic through PHP ....

Share:
17,286

Related videos on Youtube

Alexandru Florea
Author by

Alexandru Florea

Hi. Have questions? Need help?! Get in touch. JavaScript NodeJs, ReactJs, Redux, VueJs, AngularJs... Python PHP Wordpress, Magento or...any questions about SEO, Google Ads, Google Analytics, Tag Manager... I am happy to help you for FREE here!!! Just ask.

Updated on August 17, 2022

Comments

  • Alexandru Florea
    Alexandru Florea over 1 year

    I asign class to element but after loading page the class is removed.

    I use this function in my script.js

    $('#main-nav li ul li a').click(function(){
        $('#main-nav li ul li').children().removeClass('current');
        $(this).addClass('current');
    });
    

    and this in my PHP view:

     <ul id="main-nav">  <!-- Accordion Menu -->
        <li>
           <a href="#" class="nav-top-item no-submenu"> <!-- Add the class "no-submenu" to menu items with no sub menu -->
        On site
           </a>       
        </li>
    
        <li> 
           <a href="#" class="nav-top-item current"> <!-- Add the class "current" to current menu item -->
        Programs
           </a>
       <ul>
           <li><a href="<?php echo site_url('admin/country_management')?>">Manage country</a></li>
           <li><a href="<?php echo site_url('admin/channel_management')?>">Manage channel</a></li>
           <li><a href="#">Manage Comments</a></li>
           <li><a href="#">Manage Categories</a></li>
        </ul>
           </li>
    
    </ul> <!-- End #main-nav -->
    

    If I use this <li><a href="<?php echo site_url('...')?>">Manage</a></li> the class current is added but after load is removed.

    How can I add class if I use php code after load page or have you any solution ?

  • Alexandru Florea
    Alexandru Florea over 11 years
    Hi. My code is executing but after that class is removed. I need to asign class after page load or add class 1 more time to the element ul li a.