-2

hi i was making a content slider but i got stuck at this error called Unexpected token function. the console shows that i have a error at this function error: uncaught syntax error : Unexpected token function.

goNext function(){
    $('.active').removeClass('active').addClass('oldActive');
    if($('.oldActive').is(':last-child')){
        $('.slide').first().addClass('active');
    }else{
        $('.oldActive').next().addClass('active');
    }
    $('.oldActive').removeClass('oldActive');
    $('.slide').fadeOut(speed);
    $('.active').fadeIn(speed);
}

any ideas how to get this fixed...??

varun teja
  • 244
  • 3
  • 10

1 Answers1

1

The format of a JavaScript function is as so:

function name() {
code to be executed
}

In your code, you had the "name" and "function" swapped. The format of your function was like this:

name function() {
code to be executed
}

Try this instead:

function goNext(){
    $('.active').removeClass('active').addClass('oldActive');
    if($('.oldActive').is(':last-child')){
        $('.slide').first().addClass('active');
    }else{
        $('.oldActive').next().addClass('active');
    }
    $('.oldActive').removeClass('oldActive');
    $('.slide').fadeOut(speed);
    $('.active').fadeIn(speed);
}

Alternatively, if you would like the value of "goNext" to be a function, you could add a colon after goNext. See here for more info on this. NOTE: This would only be valid in an object literal.

goNext: function(){
    $('.active').removeClass('active').addClass('oldActive');
    if($('.oldActive').is(':last-child')){
        $('.slide').first().addClass('active');
    }else{
        $('.oldActive').next().addClass('active');
    }
    $('.oldActive').removeClass('oldActive');
    $('.slide').fadeOut(speed);
    $('.active').fadeIn(speed);
}
Community
  • 1
  • 1
abagh0703
  • 841
  • 1
  • 12
  • 26