<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <script>
        function adddate()
        {
            date=new Date();
            var month = date.getMonth()+1;
             var day = date.getDate();
             var year = date.getFullYear();
             if (document.getElementById('datetext').innertext =='')
             {

             document.getElementById('datetext').innertext = month + ' ' + day + ',' + year;

            }
        }
    </script>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>


    <body onload="adddate();">

    <label id="datetext"></label> 
<%--<input type="text" id="datetext">--%>
    </body>
</html>

<%--this code is working for text box but not for label..why?--%>

try 'innerHTML' instead of 'innertext'..
.
.
and BTW you should've rather posted this in javascript forum, isn't it?

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <script> function adddate() { date=new Date(); var month = date.getMonth()+1; var day = date.getDate(); var year = date.getFullYear(); if (document.getElementById('datetext').innertext =='') { document.getElementById('datetext').innertext = month + ' ' + day + ',' + year; } } </script> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body onload="adddate();"> <label id="datetext"></label> <%--<input type="text" id="datetext">--%> </body></html>

thanks its working..sorry by mistake posted here.

This question has already been answered. Start a new discussion instead.