-1

I'm getting an "unexpected token =" on the function toggleNav = function(evt){ line of the code sample below. I've went through a number of similar posts, but can't seem to see why it's throwing the error.

Any help pointing me in the right direction is much appreciated.

<script type="text/javascript"> $(document).ready(function($) { $("#nav > li > a").on("click", toggleNav); function toggleNav = function(evt){ var clicked = $(this).parent().attr("class"); $("#nav").removeClass("btn1 btn2 btn3 btn4 btn5 btn6 btn7").addClass(clicked); evt.preventDefault(); } }); </script> 
3
  • 4
    Well, if you know the syntax it seems obvious where the error is... Check your code here jslint.com Commented Jun 22, 2013 at 4:55
  • 1
    This ain't java you know. You cannot give type definition for a variable. i.e., its var x = 1 not int x = 1. Commented Jun 22, 2013 at 5:04
  • @elclanrs Thank you! Normally our other developer does the jQuery work so I'm still getting the basics down. Appreciate the advice. Commented Jun 22, 2013 at 5:19

4 Answers 4

2

Try like this

var toggleNav = function(evt){ var clicked = $(this).parent().attr("class"); $("#nav").removeClass("btn1 btn2 btn3 btn4 btn5 btn6 btn7").addClass('clicked'); evt.preventDefault(); } 

or you can directly call like

function toggleNav (evt) { var clicked = $(this).parent().attr("class"); $("#nav").removeClass("btn1 btn2 btn3 btn4 btn5 btn6 btn7").addClass('clicked'); evt.preventDefault(); } 

And also put clicked in quotes because it is an class name

Sign up to request clarification or add additional context in comments.

Comments

2

You should just replace your function toggleNav with var toggleNav, and it should be ok.

Or you could also just remove that anonymous function, and do this way :

function toggleNav (evt) { var clicked = $(this).parent().attr("class"); $("#nav").removeClass("btn1 btn2 btn3 btn4 btn5 btn6 btn7").addClass(clicked); evt.preventDefault(); } 

Comments

2

This looks more promising

$(function($) { $("#nav > li > a").on("click", function(evt) { evt.preventDefault(); var clicked = $(this).parent().attr("class"); $("#nav").removeClass("btn1 btn2 btn3 btn4 btn5 btn6 btn7").addClass(clicked); }); }); 

Comments

0

It is a syntax problem, you're using the function keyword twice there. Choose which way you want to use it. You can do either.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.