jQuery

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
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *