jQuery Selector .class, .class, .class

Introduction

The .class selector can also be used to select multiple classes.

Seperate each class with a comma.

$(".class1,.class2,.class3,...")
Parameter OptionalDescription
class Required. class of the elements to select

Select all elements with class "intro", "demo" or "end":

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $(".intro, .demo, .end").css("background-color", "yellow");
});/*  w ww  .  j  a va 2s .co  m*/
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p class="intro">This paragraph has class "intro".</p>
<p>This is a paragraph.</p>
<p class="demo">This paragraph has class "demo".</p>
<p>This is another paragraph.</p>
<p class="end">This paragraph has class "end".</p>

</body>
</html>



PreviousNext

Related