-1

For some reason my code that i am running right now is not working. I have a menu on the left hand side and it should be when someone clicks it it will stay active on that menu. However it is not working.

<head>
    <link rel="stylesheet" href="Style/style.css" type="text/css"/>
<script type="text/javascript" src="http://github.com/jashkenas/coffee-script/raw/master/extras/coffee-script.js"></script>
<script>
(function () {
    $('ul.menu li').click(function () {
        $('ul.menu li').removeClass('active');
        return $(this).addClass('active');
    });
}.call(this));
</script>

</head>
<body>
......
    <ul class="menu">
      <li><i class="fa fa-home"></i>Dashboard</li>
      <li><i class="fa fa-envelope"></i>Messages</li>
      <li><i class="fa fa-user"></i>Profile</li>
      <li class="active"><i class="fa fa-group"></i>Friends</li>
      <li><i class="fa fa-cog"></i>Settings</li>
    </ul>
  </nav>
.....

Would anyone know why this is? Thanks

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • `return $(this).addClass('active');` makes no sense. Why are you returning a jQuery object? – epascarello Feb 01 '16 at 00:13
  • Are you getting any javascript errors in your console? If so could you post them? – B. Aikey Feb 01 '16 at 00:13
  • use `document.ready` ... an IIFE is not the same thing. Your code is running before elements exist – charlietfl Feb 01 '16 at 00:15
  • 1
    github is not a CDN, they are loading it as a document.. Not a javascript mime type. As suggested, you need to change where you load this file from.. Always test a script locally to the project and then when about to go live, use a CDN such as googles. – Angry 84 Feb 01 '16 at 00:29

3 Answers3

1

You try to append click event handler to your li elements when they are not in the DOM yet - your html markup comes later.

Move your script to just before closing </body> tag. Also I would shorten it to just:

$('ul.menu li').click(function () {
    $('ul.menu li').removeClass('active');
    $(this).addClass('active');
});

By the way - do you include jQuery lib anywhere on your page? If not, the script won't work too. Include jQuery lib somewhere in the head section on your page:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.js"></script>
Nikolay Ermakov
  • 5,031
  • 2
  • 11
  • 18
1

Instead of using .call(this), you can wrap it up inside $(document)'s ready function this way:

$(function () {
    $('ul.menu li').click(function () {
        $('ul.menu li').removeClass('active');
        $(this).addClass('active');
    });
});

And remove the return statement. Also, I couldn't find jQuery added to the page. Kindly add jQuery like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

After replacing your GitHub based URL, with RawGit's URL, your final code should look like:

<head>
  <link rel="stylesheet" href="Style/style.css" type="text/css"/>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script type="text/javascript" src="http://cdn.rawgit.com/jashkenas/coffee-script/master/extras/coffee-script.js"></script>
  <script>
    $(function () {
      $('ul.menu li').click(function () {
        $('ul.menu li').removeClass('active');
        $(this).addClass('active');
      });
    });
  </script>

</head>
<body>
  ......
  <ul class="menu">
    <li><i class="fa fa-home"></i>Dashboard</li>
    <li><i class="fa fa-envelope"></i>Messages</li>
    <li><i class="fa fa-user"></i>Profile</li>
    <li class="active"><i class="fa fa-group"></i>Friends</li>
    <li><i class="fa fa-cog"></i>Settings</li>
  </ul>
  </nav>
.....
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

It looks like the page is refusing to execute the script from the github url. I ran your code locally and was having issues getting the browser use the url you provided. You could use Google's CDN,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

Or it looks like there is another stackoverflow post about getting github repos working, Link and execute external JavaScript file hosted on GitHub

Goodluck