How can I count the number of elements with same class?

后端 未结 6 615
有刺的猬
有刺的猬 2020-11-29 03:11

I have a main div in my page with a specific id. Now some input elements of the same class are present in this div. So how can I count the number o

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 03:51

    Simplest example:

    document.getElementById("demo").innerHTML = "count: " + document.querySelectorAll('.test').length;
    
        
        
        

    • Coffee
    • Milk
    • Soda

提交回复
热议问题