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
    Jun 19, 2020 @ 19:20
    mikkel
    0

    add active class to men, I actually don't know what I'm doing wrong

    Hi every one. I actually don't know what I'm doing wrong, but I want to add class active to my menu and nothing works. I've tried with razor, javascript, jqyery. here is what i have tried. What am I doing wrong and why will it not work. :D

    @foreach (var item in selection)
    
    {
    
    //string cssClass = @item.Id != CurrentPage.Id ? "" : "active";
    
    // @Umbraco.If(item.IsAncestorOrSelf(Model.Content), "active", null)
    
    // string cssClass = @item.Id != CurrentPage.Id ? "" : "active";
    
    // if (item.Children.Where("TemplateId != 0 && Visible").Count() > 0)
    
    // if (item.Name == Model.Content.Name, "active")
    
    //<li class="nav-item @(item.IsAncestorOrSelf(Model.Content) ? "active")">
    
    //var root = Model.Content.AncestorOrSelf(1);
    
    //var active = "";
    
    //if (item.Id != Model.Content.Id) { active = "active"; }
    
    //if (item.Id == Model.Content.Siblings(1).Id)
    
    //{
    
    //}
    
    <li class="@(item.IsAncestorOrSelf(Model.Content) ? "active" : null)">
    
    <a class="nav-link smooth-scroll " href="@item.Url">@item.Name</a>
    
    </li>
    
    }
    
  • Amir Khan 1289 posts 2746 karma points
    Jun 19, 2020 @ 19:34
    Amir Khan
    0

    What you have should work but are you by chance using dynamics? You'd do this in that case: class="@(item.IsAncestorOrSelf(CurrentPage) ? "active" : null)"

  • mikkel 143 posts 365 karma points
    Jun 19, 2020 @ 20:38
    mikkel
    0

    no I don't think I do, how do I do it using dynamic ?. :)

  • Amir Khan 1289 posts 2746 karma points
    Jun 19, 2020 @ 20:39
    Amir Khan
    0

    Like this, but you don't want to do that: @{ var selection = @CurrentPage.AncestorOrSelf(1).Children.Where("Visible"); }

    Any chance its a front end thing like the class being applied to an element you're not expecting?

  • mikkel 143 posts 365 karma points
    Jun 19, 2020 @ 20:43
    mikkel
    0

    not what I can see. it's a very ordinary bootstrap 4 menu, so there should be anythinh :)

  • Amir Khan 1289 posts 2746 karma points
    Jun 19, 2020 @ 20:43
    Amir Khan
    0

    What's the HTML that's being rendered?

  • mikkel 143 posts 365 karma points
    Jun 20, 2020 @ 07:52
    mikkel
    0

    now i got it to work on my 3 last menu bu not on the first 2. i really dont now what is wrong in my code. i use this code here to set the class active

    string cssClass = @item.Id != CurrentPage.Id ? "" : "active";
    
       <li class="nav-item">
           <a class="nav-link smooth-scroll @cssClass" href="@item.Url">@item.Name</a>
       </li>
    

    enter image description here

  • mikkel 143 posts 365 karma points
    Jun 19, 2020 @ 20:48
    mikkel
    0

    this i my meny

     <!-- Navbar -->
                <nav class="primary-menu navbar navbar-expand-lg navbar-text-light navbar-line-under-text bg-transparent border-bottom-0">
                    <div class="container">
                        <!-- Logo -->
                        <a class="logo ml-3 ml-md-0" href="/" title="Mikkel Pohjola">@Umbraco.Field("creatorName")</a>
                        <!-- Logo End -->
                        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#header-nav"><span></span><span></span><span></span></button>
                        <div id="header-nav" class="collapse navbar-collapse justify-content-end">
                            <ul class="navbar-nav" id="nav-active">
    
    
    
    
    
                                @foreach (var item in selection)
                                {
                                    //string cssClass = @item.Id != CurrentPage.Id ? "" : "active";
                                    // @Umbraco.If(item.IsAncestorOrSelf(Model.Content), "active", null)
                                    // string cssClass = @item.Id != CurrentPage.Id ? "" : "active";
                                    // if (item.Children.Where("TemplateId != 0 && Visible").Count() > 0)
                                    // if (item.Name == Model.Content.Name, "active")
                                    //<li class="nav-item @(item.IsAncestorOrSelf(Model.Content) ? "active")">
                                    //var root = Model.Content.AncestorOrSelf(1);
    
                                    //var active = "";
    
                                    //if (item.Id != Model.Content.Id) { active = "active"; }
    
                                    //if (item.Id == Model.Content.Siblings(1).Id)
                                    //{
    
                                    //}
    
    
                                    <li class="@(item.IsAncestorOrSelf(Model.Content) ? "active" : null)">
                                        <a class="nav-link smooth-scroll " href="@item.Url">@item.Name</a>
                                    </li>
                                }
                            </ul>
                        </div>
                    </div>
                </nav>
                <!-- Navbar End -->
    

    enter image description here

  • mikkel 143 posts 365 karma points
    Jun 19, 2020 @ 20:57
    mikkel
    0

    ahh it does some how work, but it dont show anything, it have to make bottom line under each menu when it is active i will try to se the css maybe that will fix it :D

  • Richard Hamilton 36 posts 158 karma points
    Jun 20, 2020 @ 10:37
    Richard Hamilton
    0

    Have you fixed this yet

  • mikkel 143 posts 365 karma points
    Jun 20, 2020 @ 12:09
    mikkel
    0

    not yet, but I've made it work on the last 3 navbars, but for some reason it won't work on the first 2

  • 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