javascript - Jquery: get birthday value from page and substitute with age value -
i have jsp
having table users info , there's column representing user's birthday in format yyyy-mm-dd
. want substitute birth dates actual age of user. have simple js function getage 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())) { age--; } return age; }
i've tried run after page initializing in way:
$().ready(function({ $(".age").html(getage($(this).html())); }));
where .age
class of "age" column. that's way expected work:
$(".age")
- finds cell class="age"
$(".age").html(getage($(this).html()));
- substitues html in found cell value returned getage() function argument getage current value in cell. shows nothing =(
if use smthg testing:
$(".age").html(getage("1960-08-15"));
- works correctly , substitutes birthdays in rows age calculated 1960-08-15 =)
maybe don't understand $(this)
returns in context? sure should return current element invoked selector (cell)
could give me hint?
thx lot
you should loop through each age , set html using jquery .each
$(".age").each(function(){....});
fiddle: http://jsfiddle.net/yyene/
Comments
Post a Comment