2

I'm having a problem using variables with Jquery, and i just can't figure out what the problem is. In this instance i am trying to insert a random class to my li. When i tried alerting color i can cleary see that it writes .green or .red. Following that logic i should be able to use the variable to add my class right? But it is not working. What am i doing wrong?

(function(){

        $('li').on('click', function(){
            var number = Math.floor(Math.random() * 2);
            var colorA = ['.green', '.red'];
            var color = colorA[number];

            $(this).addClass(color);
        });
    })();
1
  • 6
    Try removing the dot at the class names beginnig. Commented Dec 8, 2013 at 21:24

2 Answers 2

1

simply replace colorA with this:

var colorA = ['green', 'red'];

jQuery addClass() function accepts a class "name" not a class "selector".

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

Comments

1

Remove the Dot at the class names beginning ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.