0

I was trying to get the immediate children divs of a div with id to-append.

I found this works:

console.log($("#to-append > div").length);

But since I am using that div quite a lot, it is stored in a variable:

var $toappend = $('#to-append');

How would I go about getting the divs length using the variable?

    console.log($($toappend+" > div").length);

Throws an error

Thank you

1 Answer 1

5

You want the .children function:

console.log(toappend.children('div').length);
Sign up to request clarification or add additional context in comments.

2 Comments

I would have never guessed :)
I really think variables should be allowed to be used in direct replacement of their value

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.