2

I'm trying to add a class or a css style using the following js but getting a console error

var i = 0;

$(".question")[i].addClass("show");

get the following console log error: Uncaught TypeError: Object # has no method 'addClass'

or / and

$(".question")[i].css("display", "block");

get the following console log error: Uncaught TypeError: Object # has no method 'css'

used info from http://api.jquery.com/get/

EDIT
Still doesn't working if get rid of the variable i, and use numbers 0 or 1

2
  • Can you show your markup? Commented Jul 24, 2013 at 20:18
  • 1
    $(".question").eq(i).addClass("show"); Commented Jul 24, 2013 at 20:18

1 Answer 1

7

When you access an item from a collection with a subscript like [i], you're actually unwrapping it from the jQuery object, and accessing a raw DOM node, which doesn't have methods like addClass and css.

Use .eq() instead:

var i = 0;
$(".question").eq(i).addClass("show");
$(".question").eq(i).css("display", "block");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @bfavaretto - been scratching the head for ages on this one!

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.