1,105,331 Community Members

finding an class with a prefix.

Member Avatar
Virangya
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

suppose i have div which has a class called "suf-text", and there could be other class names like "suf-gang", "suf-hub" .. so on..
i want to trigger an onclick even on these divs.
i actually don't know how to do it..

would following do?

$('body').find()

but does find() need an regular expression ? i'm lost please help!

Member Avatar
Virangya
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok guys got it myself :)

$('body').find('div[class^="suf-"]');
Member Avatar
Airshow
WiFi Lounge Lizard
2,798 posts since Apr 2009
Reputation Points: 333 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Virangya,

Unless you need to specifically exclude elements in the document's head, then you can simplify to $('div[class^="suf-"]'); .

But as they point out in the jQuery documentation, this form of selector is slow (because internally jQuery has to use a regExp).

It's better to separate out the prefix "suf" as a class in its own right.

<div class="suf text"><div>
<div class="suf gang"><div>
<div class="suf hub"><div>

Now you can select all "suf"s with the much quicker $(".suf") .

And to select by the other part of the className:

$(".text")...;
$(".gang")...;
$(".hub")...;

Or, to be certain that you select only "text", "gang", "hub" in combination with "suf", then you can set ".suf" as the context of the other class selector:

$(".text", ".suf")...;
$(".gang", ".suf")...;
$(".hub", ".suf")...;

As the context can also be a previously-created jQuery object, you can make multiple selections of this type more efficient by pre-selecting all "suf"s:

$allSufs = $(".suf");
$(".text", $allSufs)...;
$(".gang", $allSufs)...;
$(".hub", $allSufs)...;

Airshow

Member Avatar
Virangya
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hey thanx a lot for your explanation. but, it's ok to be slow, i have another problem. with ur method it can be sloved i see.. but i don't know how to do it in previous way please explain me how to do it..?
i want to find all the classes which has a class name like "textstyle suf-pub" and 'textstyle' 'pub' can be changed but suf- is always there.. i tried regex but, i don't have enough knowledge or practice... please help!

Member Avatar
Virangya
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i mean there can be other classes assigned to that same tag..

Member Avatar
Airshow
WiFi Lounge Lizard
2,798 posts since Apr 2009
Reputation Points: 333 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

i mean there can be other classes assigned to that same tag..

Yes, that's the whole point!

Multiple classes per tag make prefixes like "suf-" unnecessary.

Airshow

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: