Reputation:
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>
Upvotes: 0
Views: 348
Reputation: 112
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.
Upvotes: -1
Reputation: 1
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>
Upvotes: 0
Reputation: 1230
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.
Upvotes: 3
Reputation: 327
document.getElementById('date').value; // Use this to get your value.
Upvotes: -1