Hi, I am writing 3 cases:
Method 1: If .test is a direct child of #testdiv
In this you can use . children
For instance:
$("#testdiv").children(".test")
This will give you an array of elements with the class 'test'. In javascript, to find the size of the array, we use the length property. So $ ("# Testdiv"). Children ("test"). Length will do your work.
Method 2: Similarly, to find all elements with class "test" in your #testdiv, you can use . Find callback. This approach should be used if you do not know the nested level of the ".test" element, because it is not optimized. For example: $ ("# Testdiv"). Find ("Test") This will give you an array of elements. Use the same .length thing to find the size of the array.
Method 3: You can also try:
$("#testdiv .test")
This will work the same as the method: 2, but it is less optimized.
Your mistake: You tried to use the following syntax
$("#testdiv.test")
This selector provides an element with class 'test' and id 'testdiv'.
thanks
source share