user3808307
user3808307

Reputation: 1549

JQuery Variable Syntax - Using variables in expressions

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

Upvotes: 0

Views: 102

Answers (1)

Skrat
Skrat

Reputation: 587

You want the .children function:

console.log(toappend.children('div').length);

Upvotes: 5

Related Questions