How to add active class to codeigniter hyperlinks? How to add active class to codeigniter hyperlinks? codeigniter codeigniter

How to add active class to codeigniter hyperlinks?


try this one.i think no need of javascript or jquery.

If you are using codeigniter then you can use URI Class.

<li class="home">    <a class="<?php if($this->uri->segment(1)=="search"){echo "active";}?>" href="<?=base_url('search')?>">        <i class="icon-search"></i>        <span>BEDRIJF ZOEKEN</span>    </a></li>

please let me know if you face any problem


I created a helper and saved it into the helper directory named as "menu_helper.php":

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');if(!function_exists('active_link')) {  function activate_menu($controller) {    // Getting the class instance.    $ci = get_instance();    // Getting the router class to actived it.    $class = $ci->router->fetch_class();    return ($class == $controller) ? 'active' : '';  }}

Then in config/autoload.php, I added "menu" as a helper on line 91.

The last step is to put the code for print the "active" class when accessing the page (i.e. Login Page):

<li class="<?php echo activate_menu('login'); ?>">      <?php echo anchor('login', 'Login'); ?> </li>


if (href === window.location.pathname) {   $('a[href='+ href +']').addClass('active');}