0

after clicking on any of the navbar options page refrehes and active tab goes to "All" tab

but i want it to stick to the option i selcted

can anyone tell me whats wrong here

<nav class="navbar navbar-default word" id="myDIV">
        <div class="navbar-header">
        <div class="col-md-12">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
           <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                        <li class="active">
                            <a href="home.php">All</a>
                        </li>
                            
                    <?php foreach( $tag_arr AS $key => $tags_name ) { ?>                    
                        
                        <li>
                            <a href="home.php?tags='.$tags_name ?>"><?php echo  ucfirst($tags_name);  ?>
                            </a>
                        </li>

                    <?php } ?>
                    
                </ul>
            </div>
        </div>
    </nav>
    


<script>
    
$( '#myDIV .navbar-nav li' ).on( 'click', function () {
    //$(this).addClass('active');
    $('#myDIV .navbar-nav').find( 'li.active' ).removeClass('active');
    $( this ).addClass( 'active' );
});

</script>
5
  • page refreshes because you change php page.. and active goes back to All because it is hardcoded in the HTML. All your JS changes will be erased on page reload Commented Oct 1, 2020 at 9:39
  • but the page is same Commented Oct 1, 2020 at 9:40
  • but you go trought a PHP action, hence you re-render the page server side Commented Oct 1, 2020 at 9:41
  • what can i do for that Commented Oct 1, 2020 at 9:41
  • have something in your page like $currentpage and then use a conditional to check in which page the code is rendered Commented Oct 1, 2020 at 9:44

1 Answer 1

1

I don't know why you have to put javascript here while your page will be redirected after click to a navbar item.

  1. You only make all tab active if there is no query string tags or tags is empty
  2. Make each tag active if there is query string tags equal to tag's name.

I didn't have a chance to test but I believe you need something like this:


<?php
$tag = $_GET["tags"];
?>

<nav class="navbar navbar-default word" id="myDIV">
        <div class="navbar-header">
        <div class="col-md-12">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
           <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                        <li
                          <?php if (!$tag) { ?>
                          class="active"
                          <?php } ?>
                        >
                            <a href="home.php">All</a>
                        </li>
                            
                    <?php foreach( $tag_arr AS $key => $tags_name ) { ?>                    
                        
                        <li
                          <?php if ($tag === $tags_name) { ?>
                          class="active"
                          <?php } ?>
                        >
                            <a href="home.php?tags='.$tags_name ?>">
                               <?php echo  ucfirst($tags_name);  ?>
                            </a>
                        </li>

                    <?php } ?>
                    
                </ul>
            </div>
        </div>
    </nav>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.