J
J
jamster2018-02-14 18:13:26
JavaScript
jamster, 2018-02-14 18:13:26

How to get div values?

There are 5 divs with text nodes inside:

<div class="counter">
    <div class="count" data-num=''>1</div>
    <div class="count" data-num=''>2</div>
    <div class="count" data-num=''>3</div>
    <div class="count" data-num=''>4</div>
  </div>

The task is as follows: take all the values, convert to a string-number, arrive at this number another number. And do the opposite, disband the result by div'am.

Basically what I got. But this is very ugly. Help me write better.

https://jsfiddle.net/r733me83/1/

Help how to do it. Or give a direction where to look for a solution.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
0
0xD34F, 2019-07-26
@jamster

setInterval(count => {
  const
    len = count.length,
    val = `${+count.map(n => n.textContent).join('') + 1}`.padStart(len, 0).slice(-len);

  count.forEach((n, i) => n.textContent = val[i]);
}, 50, [...document.querySelectorAll('.count')]);

https://jsfiddle.net/vtp4wqfo/

S
Sergey delphinpro, 2018-02-14
@delphinpro

$('.selector').each(function(){
  $(this).text(+$(this).text() + 1);
});

https://jsfiddle.net/knft23sr/1/

A
Aleksey Solovyev, 2018-02-14
@alsolovyev

https://jsfiddle.net/zrpq125n/5/

D
Dima Polos, 2018-02-14
@dimovich85

So?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question