Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element or class LIKE selector for jQuery?

For whatever reason I have these classes called .main_sub1, .main_sub2 etc. Never mind why I can't have .main .sub.

Is there a way with jQuery, sort of in the way it is possible to do with attributes, to get the classes containing main?

like image 561
David Andersson Avatar asked Feb 08 '10 10:02

David Andersson


People also ask

Is jQuery a selector of a class?

class selector selects all elements with the specific class. The class refers to the class attribute of an HTML element. The class attribute is used to set a particular style for several HTML elements. Note: Do not start a class attribute with a number.

Which selector is faster in jQuery?

ID and Element selector are the fastest selectors in jQuery.

What is element selector jQuery?

jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more.

Which is the correct jQuery selector to select all elements?

A - A jQuery Selector is a function which makes use of expressions to find out matching elements from a DOM based on the given criteria. B - jQuery selectors are used to select one or more HTML elements using jQuery. C - jQuery selectors start with the dollar sign and parentheses - $() D - All of the above.


1 Answers

Using $("[class^=main]") will select all elements whose classname starts with 'main'. Take a look at jQuery docs about selectors, there are a lot of other variations you can use, for example:

  • [class*=main] will select elements whose classname contains 'main'
  • [class~=main] will select elements whose classname has the word 'main' (delimited with spaces)
  • [class$=main] will select elements whose classname ends in 'main'
like image 81
Tatu Ulmanen Avatar answered Sep 17 '22 13:09

Tatu Ulmanen