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?


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

Recommended Answers

All 5 Replies

ok guys got it myself :)



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:


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)...;


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!

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

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.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.