kirqe
kirqe

Reputation: 2470

coffeescript on click removes class

I have a navbar with links structured this way .nav li a

This code adds "active" to "li" when I click "a". But it removes it right away.

jQuery ->
      $('.nav a').on 'click', -> 
                $(this).parent().addClass('active')

How to keep "active" on selected "li"?

ps this is what I have on jquery

$(document).ready(function(){
    $(".nav a").on("click", function () {
    $(".nav").find(".active").removeClass("active");
    $(this).parent().addClass("active");
  });
});

Upvotes: 0

Views: 243

Answers (1)

Vucko
Vucko

Reputation: 20844

To get coffescript from JS, check out js2coffee.org.

If you enter your script in it:

$(document).ready(function(){
    $(".nav a").on("click", function () {
        $(".nav").find(".active").removeClass("active");
        $(this).parent().addClass("active");
    });
});

You'll get:

$(document).ready ->
  $(".nav span").on "click", ->
    $(".nav").find(".active").removeClass "active"
    $(this).parent().addClass "active"
    return

  return

Just watch for indentation as coffescripts uses it.

Upvotes: 1

Related Questions