0

What I'm trying to do is write a function that will iterate over all different html elements which have a class name beginning with rnum- followed by a number. A complete example of a span would be <span class="rnum-293"></span>.

I then want to implement the function that I have written below and have it do the increasing effect up to each of their given numbers(specified in the class name).

Does anyone have an idea how I can achieve this? Thanks!

var count = 0;
var target = 126;
var increment = target / 198;
var elements = document.getElementsByClassName('rnum');

function calc() {
  if(count < target) {
    count += increment;
  }
  for(var i = 0; i < elements.length; i++) {
   elements[i].innerHTML = Math.round(count);
  }
}

setInterval(calc, 10)
@import url('https://fonts.googleapis.com/css?family=Bangers');

body {
  font-family: 'Bangers';
  display: flex;
  align-items: center;
  justify-content: center;
}

.rnum {
  font-size: 55px;
  margin: 50px;
}
<span class="rnum"></span>
<span class="rnum"></span>
<span class="rnum"></span>

4
  • 1
    stackoverflow.com/questions/6195373/… Commented Jan 20, 2017 at 14:50
  • 1
    you could use querySelector to select the ones begining with rnum - something like querySelector('class^=rnum-') Commented Jan 20, 2017 at 14:52
  • 1
    @Pete querySelector() only returns the first matched element. querySelectorAll() Commented Jan 20, 2017 at 14:54
  • good point @Andreas Commented Jan 20, 2017 at 14:55

1 Answer 1

2

To select the elements, use:

var elements = document.querySelectorAll('[class^="rnum-"]');

Or, if only span elements, use:

var elements = document.querySelectorAll('span[class^="rnum-"]');

I am unsure about the second part of your question. For all I see, the only issue was selecting the elements.

For the second part:

JavaScript:

    var elements = document.querySelectorAll('span[class^="rnum-"]');

function calc() {
    var el = null;
    var val = 0;
    var currentValue = 0;
    var incrementFactor = 198;
    var increment = 0;
    var maxValue = 0;

    for (var i = 0, len = elements.length; i < len; i++) {
        el = elements[i];
        maxValue = Number(el.getAttribute('data-max-val'));
        currentValue = Number(el.getAttribute('data-current-val'));
        increment = maxValue / incrementFactor;

        if ((currentValue + increment) > maxValue)
            continue;

        val = currentValue + increment;
        el.innerHTML = Math.round(val);
        el.setAttribute('data-current-val', Math.round(val));
    }
}

setInterval(calc, 10)

HTML:

<span class="rnum-198000" data-current-val="0" data-max-val="198000">0</span>
<span class="rnum-299000" data-current-val="0" data-max-val="299000">0</span>
<span class="rnum-399000" data-current-val="0" data-max-val="399000">0</span>
<span class="rnum-499000" data-current-val="0" data-max-val="499000">0</span>

Pen: http://codepen.io/anon/pen/rjyqMd?editors=1111

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help man,I'm getting there. It seems to be showing the first number then not updating, do you know why? codepen.io/apswak/pen/BpWmYV?editors=0001
Because it's always getting the value from the class and that value is not being updated. Personally, I'd use a data- attribute to store the last value.
Can use some optimizing, but you get the idea: codepen.io/anon/pen/rjyqMd?editors=1111
I've made an easier to understand approach: codepen.io/anon/pen/rjyqMd?editors=1111
Great! Thanks a lot I really appreciate it. I've accepted your answer so it's probably a good idea to update your false js code with the new. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.