java_newbie java_newbie - 1 year ago 148
Javascript Question

Jquery: get birthday value from page and substitute with age value

I have a

having a table with users info and there's a column representing user's birthday in format
. I want to substitute those birth dates with actual age of user.
So I have simple JS function getAge for it:

function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
return age;

I've tried to run it after page initializing in that way:


is a class of "Age" column. So that's the way I expected it to work:

- finds cell with class="age"

- substitues html in found cell with value returned from getAge() function where argument for getAge is current value in that cell.
but it shows nothing =(

If I use smthg like this for testing:

- it works correctly and substitutes birthdays in all rows with age calculated from 1960-08-15 =)

maybe I don't understand what
returns in my context? I was sure it should return current element which I just invoked with my selector (cell)

Could you give me a hint?

thx a lot

Answer Source

You should loop through each age and set their html using jquery .each



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download