Skip to content

Commit d51a196

Browse files
authored
Get Countup Number From Target Element (#343)
* Update countUp.ts #301 * Update countUp.ts Using parseFloat()
1 parent 0210228 commit d51a196

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/countUp.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class CountUp {
6666

6767
constructor(
6868
target: string | HTMLElement | HTMLInputElement,
69-
private endVal: number,
69+
private endVal?: number,
7070
public options?: CountUpOptions
7171
) {
7272
this.options = {
@@ -78,6 +78,9 @@ export class CountUp {
7878
this.easingFn = (this.options.easingFn) ?
7979
this.options.easingFn : this.easeOutExpo;
8080

81+
this.el = (typeof target === 'string') ? document.getElementById(target) : target;
82+
endVal = endVal == null ? this.removeSeparators(this.el.innerHTML) : endVal;
83+
8184
this.startVal = this.validateValue(this.options.startVal);
8285
this.frameVal = this.startVal;
8386
this.endVal = this.validateValue(endVal);
@@ -88,7 +91,6 @@ export class CountUp {
8891
if (this.options.separator === '') {
8992
this.options.useGrouping = false;
9093
}
91-
this.el = (typeof target === 'string') ? document.getElementById(target) : target;
9294
if (this.el) {
9395
this.printValue(this.startVal);
9496
} else {
@@ -335,4 +337,7 @@ export class CountUp {
335337
easeOutExpo = (t: number, b: number, c: number, d: number): number =>
336338
c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
337339

340+
removeSeparators(number: string): number {
341+
return parseFloat(number.replace(new RegExp(this.options.separator, 'g'), ''))
342+
}
338343
}

0 commit comments

Comments
 (0)