I have an issue with what seemed a simple task, but I'm having a brain farth, so it's not :)

I count child elements with lengthand add +1 on top of that length. This works fine and I store that in a var $trackCount

$trackCount = $carousel.find('.owl-item').length +1;

Now I want to get that variable in a .css() method which I normally have no issues with if it only needs to be a property value, but now I need to extend a property value with my var.

So I have to get the following where the number (in this example, 8) needs to be my $trackCount var.

$carousel.find('.owl-wrapper').css('counter-reset','tracks 8' );

I tried several things with wrapping $trackCount in + signs and single ' and double " quotes or a combination of them, but with no luck so far.

Edited by gentlemedia

5 Months
Discussion Span
Last Post by diafol

This doesn't work?

carousel.find('.owl-wrapper').css('counter-reset', 'tracks ' +  trackCount);

Is there a reason for prefixing $ signs on your js variables (I've taken them off here)?

Does the trackCount var need a global context?

Edited by diafol


Thanks Diafol.

I tried that and it didn't do anything and I didn't got any errors in my console related to this.
Thus I thought I would make a little Codepen demo to show you this and to my surprise in the pen it does work, so I have a conflict with something else in my code.


Is there a reason for prefixing $ signs on your js variables

Yeah not sure :) but a while ago I read somewhere that it is good practise to do it like that to distinguise jQuery objects from JS or something like that. And also that with prefixing a $ you can't pick (accidently) a reserved JS name... anyway not sure either if it is best practise or not :)

For now I have to find our where my conflict is.

Edited by gentlemedia


AFAIK, and I'm no expert. $ is used as an alias for the jQuery object. Once the jQuery object - often a DOM object or property is placed into a vanilla js variable, I don't think it is then classed as a jQuery anything, it's just bog standard object, array, value, etc.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.