Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • mikkel 143 posts 365 karma points
    Mar 04, 2019 @ 13:02
    mikkel
    0

    how do i add the bootstrap css class active to the menu when clicked

    Hi i am trying to set the css class active to the active tab but no matter what i try it dont work can some one tell me what i do wrong and what to do.

    Here is my code and the codes i have ben trying to get to work

      //Jquery code is at the bottom 
        <!-- Navigation -->
        <div id="topheader">
            <nav class="navbar navbar-expand-lg navbar-light bg-light">
                <div class="container">
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="">
                        <div>
                            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                                <ul class="navbar-nav mr-auto">
                                    <li class="nav-item active" id="top">
                                        <a class="nav-link" href="/">Forsiden <span class="sr-only">(current)</span></a>
                                    </li>
    
    
                                                                    //HERE IS MY MENU ITEM
                                    @{
                                        var selection = Model.Content.Site().Children().Where(x => x.IsVisible());
                                    }
    
                                    @foreach (var item in selection)
                                    {
                                        <li class="nav-item">
    
    
                                            <a href="@item.Url" class="nav-link">@item.Name</a>
                                        </li>
                                    }
    
    
                                                                        //HERE IS MY DROPDOWN MENU
                                    <li class="nav-item dropdown">
                                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            Undervisning
                                        </a>
                                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
    
                                            @{
                                                var selection1 = Model.Content.Site().FirstChild("undervisning").Children().Where(x => x.IsVisible());
                                            }
    
                                            @foreach (var item in selection1)
                                            {
                                                <a href="@item.Url" class="dropdown-item">@item.Name</a>
                                                <div class="dropdown-divider"></div>
                                            }
    
    
    
    
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>
    
                    </div>
                </div>
            </nav>
        </div>
    

    I have also tryit this razor code without luck

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{ var selection = CurrentPage.Site(); }
    
    @Traverse(selection)
    
    
    @helper Traverse(dynamic node)
    {
    var maxLevelForSitemap = 3;
    
    var selection = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);
    
    if (selection.Any())
    {
            <ul>
                @foreach (var item in selection)
                {
                    string cssClass = @item.Id != CurrentPage.Id ? "" : "current";
    
                    <li class="@cssClass">
                        <a class="nav-link" href="@item.Url">@item.Name</a>
    
                        @Traverse(item)
                    </li>
                }
            </ul>
    }
    }
    

    I have tryit many things to get it to work and here is some jquery i have try to use but it dont work Why??

    $('.navbar-nav li a').click(function (e) {
        //alert(e.currentTarget,"//",e.target,"//",this);
        e.stopPropagation();
        $('.navbar-nav li a').removeClass('active');
        $(this).addClass('active');
    });
    
  • Ole Martin Bakke 112 posts 624 karma points
    Mar 05, 2019 @ 17:56
    Ole Martin Bakke
    0

    I think you should go with razor. Jquery won’t work because you do a page load after the script runs, so it will reset. Does your razor run successfully or do you get an error? I think you should remove the @ in @item.Id != CurrentPage.Id I am also not sure the helper knows CurrentPage, so it might be you have to pass it in as a parameter.

  • mikkel 143 posts 365 karma points
    Mar 06, 2019 @ 07:34
    mikkel
    0

    i have solved the problem. it was bootstrap 4 that was the problem. When I switched to a bootstrap 3 menu it worked with jquery

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies