JQUERY FIND CLASS NAME CONTAINS

How can I vì chưng this:

I have object that has multiple classes. My goal is khổng lồ get class name that has string(e.g. "toaster") in it (or starting with that string) và put it in variable.lưu ý that I know only the beggining of class name & I need lớn get whole.

Bạn đang xem: Jquery find class name contains

e.g.

I have this div as my jQuery object, I only need khổng lồ put class name toaster-maker in variable className;

Again I don"t need to select object! I only need to lớn put class name in variable.


javascript jquery class variables select
Share
Improve this question
Follow
asked Aug 6 "13 at 8:12
vladvlad
1,131 3 3 gold badges 13 13 silver badges 26 26 bronze badges
1
Will that class always be written as the last one? – Spokey Aug 6 "13 at 8:16
Add a phản hồi  | 

3 Answers 3


Active sầu Oldest Votes
3

So, assuming you already have a jQuery object with that div, you could get the value of the class attribute, split the string into lớn the class names, iterate over them and see which one contains toaster:

var className = ""; $.each($element.attr("class").split(/s+/), function(i, name) });

jQuery doesn"t provide an specific function for that.

If you have sầu multiple elements for which you want to lớn extract the class names, you can use .map:

var classNames = $elements.map(function() }); }).get();

classNames will then be an array of class names.

In browser which support .classList, you could also use $.each(this.classList, ...) instead of $.each(this.className.split(/s+/), ...).

Xem thêm: " Ls Là Gì, Nghĩa Của Từ Ls, Ls — Các Trường Trong Đầu Ra Ls


Share
Improve this answer
Follow
edited Aug 6 "13 at 8:27
answered Aug 6 "13 at 8:18
Felix KlingFelix Kling
692k 157 157 gold badges 992 992 silver badges 1056 1056 bronze badges
2
function(name) should be function(key, name). Otherwise you'll use the index as the string – Spokey Aug 6 "13 at 8:22
Thanks! Another victyên ổn of jQuery's confusing callback parameter order ;) – Felix Kling Aug 6 "13 at 8:24
Add a phản hồi  | 
6

A regular expression seems lớn be more efficient here:

classNames = div.attr("class").match(/*toaster*/g)

returns all class names that contain "toaster" (or an empty array if there are none).


Share
Improve sầu this answer
Follow
answered Aug 6 "13 at 8:28
georggeorg
190k 45 45 gold badges 258 258 silver badges 348 348 bronze badges
0
Add a bình luận  | 
3

try this,

var names = $("").attr("class").split(" "); var className; $.each(names, function()) console.log(className);

fiddle is here. You can also have className as an array and push the matched class names to lớn it.


Share
Improve this answer
Follow
answered Aug 6 "13 at 8:27
Ayyappan SekarAyyappan Sekar
9,417 1 1 gold badge 15 15 silver badges 20 trăng tròn bronze badges
Add a phản hồi  | 

Your Answer


Thanks for contributing an answer lớn Staông xã Overflow!

Please be sure lớn answer the question. Provide details và nội dung your research!

But avoid

Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; baông xã them up with references or personal experience.

To learn more, see our tips on writing great answers.


Draft saved
Draft discarded

Sign up or log in


Sign up using Google
Sign up using Facebook
Sign up using Thư điện tử & Password
Submit

Post as a guest


Name
Email

Required, but never shown


Post as a guest


Name
Thư điện tử

Required, but never shown


Post Your Answer Disthẻ

By clicking “Post Your Answer”, you agree khổng lồ our terms of service, privacy policy and cookie policy


Not the answer you're looking for? Browse other questions tagged javascript jquery class variables select or ask your own question.


The Overflow Blog
Level Up: Mastering statistics with Python thả – part 4
Best practices can slow your application down
Featured on Meta
Visual kiến thiết changes khổng lồ the Đánh Giá queues
Survey questions for outdated answers
Introducing Outdated Answers project
Visit chat
Related
1419
What is the best way to add options lớn a select from a JavaScript object with jQuery?
2935
How can I change an element's class with JavaScript?
2830
How can I know which radio button is selected via jQuery?
2128
How can I select an element with multiple classes in jQuery?
1946
Get selected value in dropdown danh mục using JavaScript
1322
How can I select an element by name with jQuery?
1672
Convert form data to JavaScript object with jQuery
2367
Get selected text from a drop-down list (select box) using jQuery
7421
How khổng lồ check whether a string contains a substring in JavaScript?
845
jQuery get value of select onChange
Hot Network Questions
Studies comparing motorway vs bike lane costs
Curiosity và Perseverance landing - with so much dust blown everywhere, what's the point of the skycrane?
Generalise perfect numbers
Should I quit a PhD with little supervision at a prestigious university & start a new PhD?
C++ Fast Fourier transform
What is the opposite of 'owe'?
Backyard wildlife zoom question factoring distance
Saturn V's blueprints
Should I address a "Charge de Recherche" or "Directeur de recherche" in France as "Prof."?
What does "reefer" signify in Reynolds' books?
frater < "fere" + "alter"?
How can a transistor amplify current in a circuit?
What does "some type of sheep meat" mean
Accidentally saw a private photo lớn of my trùm on the workplace tệp tin storage
Is there a known classification of regular multiplicity-miễn phí permutation groups?
Why don’t airplanes use turn signals?
Why bởi vì we assume that PHB rules apply to monsters?
What are the differences between "Deva", "Bhagavan", và "Ishvara"?
Speeding up FactorInteger for hàng hóa of two primes
Optocoupler with the same power supply
Is Scrooge McDuck immortal?
Would we see aliens on Europa?
Delivery of minerals to Earth from the asteroids by artificial meteorite or solid metal glider
What is offensive about the thẻ "Stone-Throwing Devils"? more hot questions
Question feed
Subscribe lớn RSS
Question feed

To subscribe to lớn this RSS feed, copy and paste this URL into lớn your RSS reader.


lang-js
Staông xã Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help Smartphone
Products Teams Talent Advertising Enterprise
Company About Press Work Here Legal Privacy Policy Terms of Service Liên hệ Us
Stack Exchange Network Technology Life / Arts Culture / Recreation Science Other
Stack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters trò chơi Development
TeX - LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering
Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica
Salesforce ExpressionEngine® Answers Stack Overflow em Português Blender Network Engineering Cryptography Code Review
Magento Software Recommendations Signal Processing Emacs Raspberry Pi Stack Overflow на русском Code Golf
Stack Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance & Testing Sound Design
Windows Phone more (28)
Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Worldbuilding Video Production
Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia Law Physical Fitness Gardening & Landscaping
Parenting more (10)
English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language
Chinese Language French Language German Language Biblical Hermeneutics History Spanish Language Islam
Русский язык Russian Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Puzzling
Motor Vehicle Maintenance & Repair Board & Card Games Bricks Homebrewing Martial Arts The Great Outdoors Poker
Chess Sports more (16)
MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology
Computer Science Philosophy Linguistics Psychology & Neuroscience Computational Science more (10)
Meta Staông chồng Exchange Staông xã Apps API Data
Blog Facebook Twitter LinkedIn Instagram

site thiết kế / hình ảnh © 2021 Staông xã Exchange Inc; user contributions licensed under cc by-sa. rev 2021.3.3.38694


Stachồng Overflow works best with JavaScript enabled
*