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 of these elements of same class in this div
using jQuery?
We can get text from multiple elements with the same class in Selenium webdriver. We have to use find_elements_by_xpath(), find_elements_by_class_name() or find_elements_by_css_selector() method which returns a list of all matching elements.
Different Elements Can Share Same Class.
To count all HTML elements, we use length property. The length property is used to count number of the elements of the jQuery object.
With jQuery
you can use
$('#main-div .specific-class').length
otherwise in VanillaJS (from IE8
included) you may use
document.querySelectorAll('#main-div .specific-class').length;
document.getElementsByClassName("classstringhere").length
The document.getElementsByClassName("classstringhere")
method returns an array of all the elements with that class name, so .length
gives you the amount of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With