How to Loop Through Elements with the Same Class in jQuery
In this tutorial, we are going to see how to loop through elements with the same class in jQuery. You can easily use the method each() of jQuery to iterate over components which have the same class and execute actions in reference to a particular condition.
How to Loop Through Elements with the Same Class in jQuery
<html> <head> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> </head> <body> <ul> <li class="languages"> JavaScript </li> <li class="languages"> HTML </li> <li class="languages"> CSS </li> <li class="languages"> PHP </li> </ul> <script> $('.languages').each(function(i){ var languages= $(this).html(); console.log(languages); }); </script> </body> </html>
Output:
JavaScript HTML CSS PHP[st_adsense]