[Solved] Simple javascript add 1 to a number and output in span html


When each() method is called, it iterates over the DOM elements that are part of the object (JQuery).

this keyword refers to the current element. Where text function can be used to retrieve the text of that element, so simply having a inner callback function to return the update text (increment by 1) would update the value and show

$('.eventYear').each(function() {
  $(this).text(function() {
    return +this.textContent + 1;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<h3>TITLE</h3>
12 DEC <span class="eventYear">2016</span> <br>
12 DEC <span class="eventYear">2015</span> <br>
12 DEC <span class="eventYear">2014</span> <br>
12 DEC <span class="eventYear">2013</span>
</div>

1

solved Simple javascript add 1 to a number and output in span html