2

I am trying to change the color of all the <li> links on web app using jQuery, but don't know why it is not working?

I have my style statement at the top in my layout.cshtml page, and the jQuery function at the bottom.

   <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Contoso University</title>

    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)

    <style>
         li {color: blue;}
        .emphasis {color: indianred;}
    </style>

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Contoso University", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li> 

                    @if (User.Identity.IsAuthenticated)
                    {
                        if (!User.IsInRole("Instructor"))
                        {
                            <li>@Html.ActionLink("Students", "Index", "Student")</li>
                        }
                        <li>@Html.ActionLink("Courses", "Index", "Course")</li>
                        <li>@Html.ActionLink("Instructors", "Index", "Instructor")</li>
                        if (!User.IsInRole("Student") && !User.IsInRole("Instructor"))
                        {
                            <li>@Html.ActionLink("Departments", "Index", "Department")</li>
                        }
                        if (!User.IsInRole("Student"))
                        {
                            <li>@Html.ActionLink("Enrollments", "Index", "Enrollment")</li>
                        }
                    }

                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - Contoso University</p>
        </footer>
    </div>

    @*<script>
        $('li').addClass('emphasis');        
    </script>*@

    <script>
        $(function () {
            $('li').addClass('emphasis');
        });
    </script>

</body>
</html>

What is it that I am doing wrong?

Truecolor
  • 437
  • 2
  • 6
  • 22

4 Answers4

2

Here are a few options...

JavaScript

If you're trying to color the bullet, working jsfiddle

$('li').addClass('emphasis');

If you're trying to color the hyperlink, working jsfiddle

$('li a').addClass('emphasis');

If you're trying to color both the bullet and the hyperlink, working jsfiddle

$('li a, li').addClass('emphasis');

HTML

<li><a href="http://www.google.com">TEST 1</a></li>
<li><a href="http://www.google.com">TEST 2</a></li>
<li><a href="http://www.google.com">TEST 3</a></li>
<li><a href="http://www.google.com">TEST 4</a></li>
<li><a href="http://www.google.com">TEST 5</a></li>

CSS

li {color: blue;}
.emphasis {color: indianred;}
Rick Burns
  • 1,538
  • 16
  • 20
1

So, after a lot of trials & errors, found out I was using the wrong selector. Posting the answer just in case if someone else has this issue.

 <script>
        $('.navbar-inverse .navbar-nav > li > a').css('color', 'red');
 </script>
Truecolor
  • 437
  • 2
  • 6
  • 22
0

Ensure that you wrap your <script> code within a document-ready block so that it executes when your page (and dependencies) have been loaded :

<script>
    // This will not execute until jQuery (and your page) have been loaded
    $(function(){
        $('li a').addClass('emphasis');       
    });
</script>
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • Nothing worked. I was only able to change the color of the University, using the `.navbar-brand` class. – Truecolor Feb 28 '17 at 15:47