0

I am very new on Web programming. Currently I try to use JavaScript get input text value, but do not know why only return undefined value. Please help. Thank you so much~~~ Below it's my code:

<!DOCTYPE HTML>
<html>

<head>
<script type="text/javascript">
function CurrentDate(){

var dateobj= new Date();

var date = dateobj.getDate(); 
var day = dateobj.getDay();
var month = dateobj.getMonth();
var year = dateobj.getFullYear();

}

function myScript(){

document.myform.date.value = CurrentDate(date);
document.myform.month.value = CurrentDate(month);
document.myform.day.value = CurrentDate(day);
document.myform.year.value = CurrentDate(year);

}
</script>
</head>


<body onload="myScript()">
<form name="myform" id="myform">
<label for ="date">date: </label>
<input id="date" type="text" name="date">
<label for ="month">month: </label>
<input id="month" type="text" name="month">
<label for ="day">day: </label>
<input id="day" type="text" name="day">
<label for ="year">year: </label>
<input id="year" type="text" name="year">

</form> 

</body>
</html>

4 Answers 4

3

javascript has functional scope so the following is what's going on with your code

function CurrentDate(){
    // everything being declared in here is only
    // going to exist until the end of the function
    var dateobj= new Date();
    var date = dateobj.getDate(); 
    var day = dateobj.getDay();
    var month = dateobj.getMonth();
    var year = dateobj.getFullYear();

}

function myScript(){
    // javascript won't die if you pass in 
    // parameters, they will either be used or ignored by
    // the method that is called
    document.myform.date.value = CurrentDate(date);
    document.myform.month.value = CurrentDate(month);
    document.myform.day.value = CurrentDate(day);
    document.myform.year.value = CurrentDate(year);

}

what you probably want to do is remove the current date function and make your code look something like this.

function myScript(){
    var dateobj= new Date();
    document.myform.date.value = dateobj.getDate();
    document.myform.month.value = dateobj.getMonth();
    document.myform.day.value = dateobj.getDay();
    document.myform.year.value = dateobj.getFullYear();

}

My jsfiddle showing how the changes would work
Douglas Crockford's web page will provide you with resources to learn more about javascript.

Sign up to request clarification or add additional context in comments.

Comments

0

You try it...

<head>
    <script type="text/javascript">
        function CurrentDate(params) {
            var dateobj = new Date();
            if (params == date) {
                return dateobj.getDate();
            } else if (params == month) {
                return dateobj.getMonth();
            } else if (params == year) {
                return dateobj.getFullYear();
            } else if (params == day) {
                return dateobj.getDay();
            } else {
                alert('Input invalid !');
            }
        }

        function myScript() {

            document.myform.date.value = CurrentDate(date);
            document.myform.month.value = CurrentDate(month);
            document.myform.day.value = CurrentDate(day);
            document.myform.year.value = CurrentDate(year);

        }
    </script>
</head>


<body onload="myScript()">
    <form name="myform" id="myform">
        <label for ="date">date: </label>
        <input id="date" type="text" name="date">
        <label for ="month">month: </label>
        <input id="month" type="text" name="month">
        <label for ="day">day: </label>
        <input id="day" type="text" name="day">
        <label for ="year">year: </label>
        <input id="year" type="text" name="year">

    </form> 

</body>

1 Comment

have you tested it.. cuz i can tell without testing that you will get something like.. date is undefined.. cuz it is a undeclared variable... if you want your above code to work.. quote your params and args to make all of them string..
-1
document.getElementById('date').value; // Use this to get your value.

3 Comments

his result is displaying as undefined, mainly because the function he is calling isn't returning anything
I pointed him to how to correctly call an elements value. Also all he would have to do is set this as a variable and return that variable. Example: function getTheDate() { return date; } | function getTheMonth() { return month; } I do not respect to be downvoted for explaining the proper way to call an elements value.
you're example does show a good way access or set the value of an input field; however, the question from the submitter is why their value is returning undefined. showing them a good way of accessing a value is one thing but helping them understand the issue is another. btw because it is custom to have unique id names for elements you could technically access the date field without using the document.getElementById method. checkout my jsfiddle link for an example
-1

Try document.getElementByID("date").value To get the value.

Also think maybe about using jquery to make it a little easier $("#date").val() will get or set an input value.

1 Comment

In this instance his main issue is with the fact that he doesn't have a full grasp on the concept of functional scope and how functions work in general... jquery wouldn't help out here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.