kira423
kira423

Reputation: 427

Setting value with Javascript

I have this code:

<html>
<head>

<script type="text/javascript">

/***********************************************
* Drop Down Date select script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/

var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

function populatedropdown(dayfield, monthfield, yearfield){
     var today=new Date()
     var dayfield=document.getElementById(dayfield)
     var monthfield=document.getElementById(monthfield)
     var yearfield=document.getElementById(yearfield)
     for (var i=0; i<31; i++)
         dayfield.options[i]=new Option(i, i+1)
     dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
     for (var m=0; m<12; m++)
         monthfield.options[m]=new Option(monthtext[m], monthtext[m])
     monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
     var thisyear=1920;  // fixed start year of 1900
     var nowyear = 1994;
     var diff = nowyear - thisyear +1; // number of years from 1900
     for (var y=0; y<diff; y++){
         yearfield.options[y]=new Option(thisyear, thisyear)
         thisyear+=1
     }
}
</script>

<script>
function getcombined(){
    var year = document.getElementbyId("yeardropdown").value;
    var month = document.getElementById("monthdropdown").value;
    var day = document.getElementById("daydropdown").value;
    var combineddob = year + "-" + "month" + "-" + day;
    document.getElementById("hidden1").value=combineddob
}
</script>


</head>

<body>
<form action="" name="someform">
<select id="daydropdown" name='day' value="daydropdown">
</select> 
<select id="monthdropdown" name='month' value="monthdropdown">
</select> 
<select id="yeardropdown" name='year'  value="yeardropdown">
</select> 
<input type='hidden' id='hidden1' name='dob' value="" />
<input type='submit' />
</form>

<script type="text/javascript">

window.onload=function(){
    populatedropdown("daydropdown", "monthdropdown", "yeardropdown")
}
</script>


</body>
</html>

I need the <input type='hidden' id='hidden1' name='dob' value='' /> to update to var combineddob when the form is submitted.

I have tried several methods, but I do not know much about javascript so I am not good with these kinds of fixes.

I may be over looking something, but I have not yet figured out what is wrong here.

Upvotes: 1

Views: 282

Answers (3)

stewbasic
stewbasic

Reputation: 841

If you want something to happen before a user submits the form, you should set the onsubmit property of the form. In this case:

<script>
<!--
function getcombined(){
    var year = document.getElementById("yeardropdown").value;
    var month = document.getElementById("monthdropdown").value;
    var day = document.getElementById("daydropdown").value;
    var combineddob = year + "-" + month + "-" + day;
    document.getElementById("hidden1").value=combineddob;
    return true;
}
-->
</script>


</head>

<body>
<form action="" name="someform" onsubmit='return getcombined();'>

I've added "return true" to the function to indicate that the form should actually submit afterwards. Also I fixed two typos, namely a lower case b in getElementbyId and a missing semicolon.

Upvotes: 2

Koerr
Koerr

Reputation: 15723

First of all:

var year = document.getElementbyId("yeardropdown").value;

to:

var year = document.getElementById("yeardropdown").value;

Add onclick in submit button:

<input type='submit' onclick="getcombined();alert(hidden1.value);" />

Upvotes: 1

Travis J
Travis J

Reputation: 82267

change your html

<input type='submit' />

to this

<input type='submit' id="submitButton" />

and in your script

window.onload = function(){
    populatedropdown("daydropdown", "monthdropdown", "yeardropdown");
    var submitBtn = document.getElementById("submitButton");
    submitBtn.onsubmit = function(){getcombined();};
};

This will allow you to run the function in onsubmit when the submit button is clicked. It will in turn call the function getcombined(); and then submit the form.

EDIT

Perhaps you should change these two lines:

var combineddob = year + "-" + "month" + "-" + day;
document.getElementById("hidden1").value=combineddob

to

var combineddob = year + "-" + month + "-" + day;
document.getElementById("hidden1").value=combineddob;

and

var year = document.getElementbyId("yeardropdown").value;

to (note that getElementbyId is not a function)

var year = document.getElementById("yeardropdown").value;

Upvotes: 1

Related Questions