jquery call a class

jQuery Call a Class

If you want to call a class in jQuery, you can use the $(".classname") selector.

Example


$(document).ready(function(){
  $(".classname").click(function(){
    $(this).hide();
  });
});

The above code will hide the element with the class "classname" when it is clicked.

You can also use other selectors like $("#idname") for calling an element with an ID, $("element") for calling all elements of a certain type (e.g. $("p") for all paragraphs), or $("[attribute='value']") for calling elements with a certain attribute value (e.g. $("[type='text']") for all text input fields).

Another way to call a class is by using the .class() function instead of the $() function.

Example


$(document).ready(function(){
  $(".classname").class(function(){
    $(this).hide();
  });
});

The above code will also hide the element with the class "classname" when it is called.

There are many ways to call elements in jQuery, but using the selector is the most common and flexible way.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe