-6

In an application I'm working on. The code outside if then isn't working. How should I achieve that target. For example when I try to place first two lines outside if else. It doesn't work.

Also how can I put repetitive code in some variable and call it with if else. For example this one $(this).before(leftArrowHTML);

$('.lessonNavigation ').on('click', 'li', function () {
if ($(this).is('.assess')) {
    $('.lessonNavigation li').not('.assess').attr('class', 'dn');
    $('.lessonNavigation li.assess').attr('class', 'dn assess');
    $(this).attr('class', 'activeLesson assess');
    $(this).prev().addClass('expanded left');
    $(this).next().addClass('expanded right');
    $(this).before(leftArrowHTML);
    $(this).after(rightArrowHTML);

} else if ($(this).is('.new')) {
    $('.lessonNavigation li').not('.assess').attr('class', 'dn');
    $('.lessonNavigation li.assess').attr('class', 'dn assess');
    $(this).attr('class', 'activeLesson');
    $(this).next().attr('class', 'expanded right brw');
    $(this).next().next().attr('class', 'expanded right');
    $(this).before(leftArrowHTML);
    $(this).after(rightArrowHTML);
    $('.arrow.left').attr('class', 'inactiveRightArrow');
}
});
  • close you bracket properly if ($(this).is('.assess')){ and use else if instead of else. – rishal Jun 10 '14 at 02:49
  • Most likely you should put those first two lines *above* the if, not below it; because the rest of the code may make changes to the document. – Ja͢ck Jun 10 '14 at 03:10
  • 1
    _"Code outside of if then doesn't work"_ ~ but you don't show any code outside of an if/then. Are you trying to tell us that your `click` handler is not working the way you want? Please write a better question that explains what you're trying to achieve. – Sparky Jun 10 '14 at 03:11

2 Answers2

1

else does not take a condition, you must use else if, also you are missing a closing parenthesis.

} else if ($(this).is('.new')) {

EDIT: you are also missing a closing bracket on your original if statement

if ($(this).is('.assess')) {

"elseif" syntax in JavaScript

Community
  • 1
  • 1
Daniel
  • 1,403
  • 1
  • 14
  • 30
  • 1
    @user3693250, please be more careful when posting your code. Your mistake wasted this user's time for answering. An answer you rendered as meaningless by subsequently editing your question with his code. – Sparky Jun 10 '14 at 03:08
1

Just move them outside the if, but not outside the click callback, which is providing a value for this.

$('.lessonNavigation ').on('click', 'li', function () {

    if ($(this).is('.assess')) {
        $(this).attr('class', 'activeLesson assess');
        $(this).prev().addClass('expanded left');
        $(this).next().addClass('expanded right');
    } else if ($(this).is('.new')) {
        $(this).attr('class', 'activeLesson');
        $(this).next().attr('class', 'expanded right brw');
        $(this).next().next().attr('class', 'expanded right');
        $('.arrow.left').attr('class', 'inactiveRightArrow');
    }

    $('.lessonNavigation li.assess').attr('class', 'dn assess');
    $('.lessonNavigation li').not('.assess').attr('class', 'dn');
    $(this).before(leftArrowHTML);
    $(this).after(rightArrowHTML);
});
user229044
  • 232,980
  • 40
  • 330
  • 338