user3188200 user3188200 - 4 months ago 11
jQuery Question

jquery modify each class html

I am new to jquery, it is taking me too much time to figure this out.

HTML

<div class="date">12-02-2012</div>
<div class="date">14-02-2012</div>


i need to save the value of the respective div in variable then change it for the respective tag.

var1 = '12-02-2012'
var2 = '14-02-2012'

<div class="date">12- Number(var1.split('-')[1])+1 -2012</div>
<div class="date">14- Number(var2.split('-')[1])+1 -2012</div>


Instead of using var1 and var2, i need it to be something like foreach element that has class="date".

Answer

You can do like this:

$('div.date').each(function(){
    var date = this.innerHTML.split('-').map(Number); // convert that string into a array of numbers
    date[1] = date[1] + 1; // add +1 in the middle number
    this.innerHTML = date.join('-'); // put it back into the div
});

jsFiddle: https://jsfiddle.net/qfqkt98r/

Comments