jQuery select by class With Example

jQuery select by class name allows us to find a specific HTML element with the value of its class attribute.

jQuery select by class

Here is the general syntax for using the jQuery select by class name:

$(".classname")

ID could be any value provided in the id attributes of the HTML element. You should use period character ( .) followed by the class name specified in the html element.

$(".myClass"): This will select all HTML elements with class name equal to “myClass”.

For example if there is a HTML element in the document like <div class="myClass"> some text </div>, $(“.myClass”) will select this div and do the intended action on it. All the manipulations will affect only the elements specified with the given class name in the selector.

jQuery select by class example

This example demonstrates the use of jQuery select by its class name and changing the CSS style of the selected element.

You can see that <div> element with class=”class2″ is selected for manipulation and some other styles are added to it.

Below image shows the output page rendered by the browser.

jQuery Select by Class Demo

jQuery select by class summary

jQuery class selector makes it easy to apply CSS class to any number of elements in the html document. This selector is very helpful in keeping uniformity of certain elements.

By admin

Leave a Reply