1

Good day.

When i use code:

$(this).children('.test')[0].css('display','none');

i get error Uncaught TypeError: Cannot call method 'css' of undefined

Why i get it error an how write aright?

2 Answers 2

4

You probably need to use eq(0) since [0] returns plain DOM element and it does not have css method on it.

$(this).children('.test').eq(0).css('display','none'); //or .hide()

or

$(this).children('.test:eq(0)').css('display','none');//or .hide()

or

$(this).children('.test')[0].style.display = 'none';

Also you error means that it could any .test which are children of the context [You are accessing css method on undefined since your selector did not return anything]. Note that children will only look for the direct descendants, if you want to look deep you can use find. If your selection had passed and returned the element then your code would have returned [element] has no method css. The above snippets (first 2) using jquery will suppress these errors.

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

Comments

0

or

$(this).children('.test').eq(0).hide()

Comments

Your Answer

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