4

I have a class suffix named fadecontent in a joomla 3.0 website, and i have to loop trough all div's that have this class suffix using the $.each() function.

I tried this but it seems not to work with class suffixes. joomla docs: class suffix

$('.fadecontent').each(function(i, obj) {

with class suffix i mean this:

<div class="class classSuffix">exemple</div>
<!--example in my code-->
<div class="moduletable fadecontent">content</div>

How to archieve this?

EDIT: the script is in the <HEAD> tag

Community
  • 1
  • 1
JoJo
  • 806
  • 1
  • 13
  • 26
  • Your use of *suffix* is ambiguous. Do you mean you want to match only *part* of a class? Otherwise, with the code you provided, `$(".fadecontent")` should match the elements you're looking for. – Frédéric Hamidi Aug 26 '14 at 19:21
  • This question is poorly worded but I think it is a duplicate? http://stackoverflow.com/questions/2220851/element-or-class-like-selector-for-jquery – catalyst294 Aug 26 '14 at 19:22
  • Did you remember document.ready, including jQuery etc. as what you've got should work fine -> http://jsfiddle.net/uv20hkry/ – adeneo Aug 26 '14 at 19:22
  • `$('.fadecontent')` should select `
    content
    `. Problem lies elsewhere. May be missing `$(document).ready()`.
    – Salman A Aug 26 '14 at 19:28
  • @Jojo, from the link you posted it looks like you want to match an actual suffix (in the etymological sense) of a class. `filter()` into a regex match against `this.className`. Look around, there probably are lots of material dealing with this problem on the web. – Frédéric Hamidi Aug 26 '14 at 19:30
  • You realize that what you're describing as "suffix" is in fact a way Joomla attaches classes suffixed, as in separated with a underscore, not a space? Are you sure you're getting this right ? – adeneo Aug 26 '14 at 19:30
  • @SalmanA when i did this whitout class suffix, but just by using $('.section) it worked fine – JoJo Aug 26 '14 at 19:30
  • @JoJo: Can you post the output of `$('.section').each(function() { console.log(this.className); })`? – Salman A Aug 26 '14 at 19:34
  • @JoJo, I found a couple of questions that might interest you: [How to write a selector to find elements with a class ending with a given substring?](http://stackoverflow.com/q/17417474/464709) and [Select div that ends with a class](http://stackoverflow.com/q/7046164/464709). – Frédéric Hamidi Aug 26 '14 at 19:37
  • Ok, it seems like i messed things up while i was testing all these answers. i backed the script up, tested it again and it worked. thanks everyone! – JoJo Aug 27 '14 at 05:58

2 Answers2

3

I'm not sure I understand what you mean by class suffixes. in your example

<div class="moduletable fadecontent">content</div>

That div has both the class moduletable and fadecontent. So this should loop through all the divs with the class fadecontent

$('.fadecontent').each(function() { console.log('Do Something here'); });

If this doesn't achieve what you're looking for, can you post more of your code so we might be able to see any other errors?

willcwf
  • 712
  • 7
  • 10
  • see update, i added joomla docs class suffix for info. i don't get any errors, just nothing happens. – JoJo Aug 26 '14 at 19:27
  • 1
    Ok, it seems like i messed things up while i was testing all these answers. i backed the script up, tested it again and it worked. thank you very much! – JoJo Aug 27 '14 at 06:00
0

If you use JQuery's special selectors such as this:

$('[class$="yoursuffix"]')

It should return elements whose classes contain your suffix pattern. You can read more about this here

omdel
  • 911
  • 1
  • 9
  • 13
  • that doesn't limit it to a suffix. That'll pick up any element with `yoursuffix` anywhere in the `class` attribute. – cookie monster Aug 26 '14 at 19:48
  • That's the "starts-with" selector, which wouldn't make sense for a suffix. And even if you use the "ends-with" selector, it'll fail if the targeted class is not last in the list of classes. – cookie monster Aug 26 '14 at 21:02
  • indeed, this does not work. the suffix is at the end. but sometimes, there are multiple suffixes. still thanks ;) – JoJo Aug 27 '14 at 06:00