Nectar,
I need to add commas to the milestone counter when it counts to the thousands. I found a script for this but where do I but this?
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
I've dequeued the script from Salient and enqueued my own version (nectar-milestone2), added onUpdate and I'm seeing no added commas. Any idea what I'm doing wrong?
I receive this error in Chrome's inspector:
Uncaught TypeError: undefined is not a function
Which is referring to the line underneath //animation:
From the code it seems that the each function is not able to find the div containers inside the div with a class name of .nectar-meilstone2 . Can you make sure that such a div exists and the appropriate class name is there.
Oh, I changed the divs back to nectar-milestone. But it still doesn't work.
I added the onUpdate function above with meaningless content inside because it seems to require onUpdate. Not sure how the author of this post resolved it, but it seems to make no difference to me. Likely because of the error I mentioned (Uncaught TypeError: undefined is not a function).
Here is the code I've added to the child theme's functions.php:
The /js/init.js I'm calling is the one I've shown you in my previous post. I can't work out why it seems unable to identify 'nectar-milestone2' when I'm enqueuing it here. If you can point me in the right direction I would be extremely grateful. Thanks Tahir!
Ugh I took 15 minutes to have a look this morning sadly I can't concentrate more time on it but maybe you can finalise it by yourself so here is my solution
Add this function in the milestone code block:
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
Then in I edited the milestone counter to the following:
For now it does what it does but because the numberWithCommas functions stringifies the value you need a way to round the number. cause it will now show it like the following image does:
But it does work for most numbers it just sometimes does this when the number has so many different numbers
Could someone tell me where to specifically add these code blocks? I'm hoping to be able to display numbers bigger than just three digits, with commas. Thank you!!
Hey Johnny,
https://github.com/mhuggins/jquery-countTo this is the plugin used for the counting in the milestone - there's a function that you can use
that can be passed to the countTo inside of the init.js file (located in the js folder) at this section:
that's where the logic should go, though you won';t be able to just paste that function in - it would require some integration.
Cheers :)
Thanks Nectar, I didn't use the "onUpdate". Below is the code I used and it seems to work.
/***************** Milestone Counter ******************/
if(!$('body').hasClass('')) {
$('.nectar-milestone').each(function() {
$(this).appear(function() {
var $endNum = parseInt($(this).find('.number').text());
$(this).find('.number').countTo({
from: 0,
to: $endNum,
speed: 1500,
refreshInterval: 30,
formatter: function (value, options) {
return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
}
});
},{accX: 0, accY: 0});
});
}
I'm trying to same thing.
I've dequeued the script from Salient and enqueued my own version (nectar-milestone2), added onUpdate and I'm seeing no added commas. Any idea what I'm doing wrong?
I receive this error in Chrome's inspector:
Uncaught TypeError: undefined is not a function
Which is referring to the line underneath //animation:
$(this).appear(function() {
Any help would be awesome, thanks!
Nathan
Hey Nathan!
From the code it seems that the each function is not able to find the div containers inside the div with a class name of .nectar-meilstone2 . Can you make sure that such a div exists and the appropriate class name is there.
Thanks
ThemeNectar Support Team
Oh, I changed the divs back to nectar-milestone. But it still doesn't work.
I added the onUpdate function above with meaningless content inside because it seems to require onUpdate. Not sure how the author of this post resolved it, but it seems to make no difference to me. Likely because of the error I mentioned (Uncaught TypeError: undefined is not a function).
Here is the code I've added to the child theme's functions.php:
The /js/init.js I'm calling is the one I've shown you in my previous post. I can't work out why it seems unable to identify 'nectar-milestone2' when I'm enqueuing it here. If you can point me in the right direction I would be extremely grateful. Thanks Tahir!
I've just disabled it for now.
Can I suggest commas in milestones as a feature request for the future? :)
Hey Nathan!
Yes it has been added to the wishlist :) .
Thanks
ThemeNectar Support Team
Ugh I took 15 minutes to have a look this morning sadly I can't concentrate more time on it but maybe you can finalise it by yourself so here is my solution
Add this function in the milestone code block:
Then in I edited the milestone counter to the following:
For now it does what it does but because the numberWithCommas functions stringifies the value you need a way to round the number. cause it will now show it like the following image does:
But it does work for most numbers it just sometimes does this when the number has so many different numbers
Hey Patrick, thanks for posting that! It will be helpful for others for the time being :)
Cheers
Could someone tell me where to specifically add these code blocks? I'm hoping to be able to display numbers bigger than just three digits, with commas. Thank you!!
Hey Travis!
As patrick mentioned it is not a 100% working solution so we would advise against it.
Thanks
ThemeNectar Support Team
OK thank you Tahir!
This seems to do the trick for me. Hopefully will help someone else.
Hi I what to put decimal and comma separators to the milestone counter. How can I do that and where to put this code
Can you not just replace content with CSS? For example:
.nectar-milestone .number span{
display: none;
}
.nectar-milestone .number:after {
content: '1,500,000';
}
Has anyone solved this problem? Decimals still only show on mobile, but not on desktop (using Salient 8.5.3).
I tried all the JS snippets below but haven't solved the issue so far.
Thanks.
Solution is easy (at least I didn't find any downsides)
If u r using Salient
1, Open: \wp-content\themes\salient\js\init.js
2, Go to line #2404
3, Change:
to:
4, Couple lines below find this:
and change like this:
5, Save file and send it to FTP (probably you will need to refresh couple of time your browser or clear cache but it should work.
Now, if you put for ex. 99.8 it will count with decimal up to 99.8, if you set value to for ex. 95 it will not show decimals.
If you would like to always count with decimal even if value is for ex. 95, just skip this part of code in step 3:
and in step 4 keep original code and just change 0 to 1 after $endNum
It's kind of shame that it wasn't been fixed for last 4 years, but maybe there are some kind of downsides with this fix. If so please let me know.
ps. I didn't needed decimals in bar countdown so I didnt fixed it but I think that should be similar.