Keluang Yang Ko Kenal
Keluang Yang Ko Kenal

Reputation: 86

Can#t take the value to put into textbox

I am new to Javascript. I built a form that consists of a dropdown text box. I successfully take the value from text box using Javascript but I failed to pass the value to the text box.

Here is my code..

<script type="text/javascript">
var year;

function changeText(elemid) {
    //take value from dropdownlist 
    var ind = document.getElementById(elemid).value;

    //calculate the age 
    var birthday = +new Date(ind);
    var result = (Date.now() - birthday) / (31557600000);

    //put into age textbox
    document.getElementById("date").innerHTML=Math.floor(result); 
}
</script>
<select name="year" id="year" onChange="changeText('year')"class="span4">
    <option value="YYYY">YYYY</option>
    <option value="1988">1988</option>
    <option value="1989">1989</option>
    <option value=""></option>
</select>
<br><br>
<b>Age:</b><br>
<div id="age">this line works</div>
<input type="text" class="span2" id="date" name="date">

Upvotes: 0

Views: 50

Answers (2)

Anto Subash
Anto Subash

Reputation: 3215

you have to use

document.getElementById("date").value = Math.floor(result); 

here is the fiddle http://jsfiddle.net/b5JZM/

Upvotes: 0

yakiang
yakiang

Reputation: 1634

for a text box, here's how to change its value:

document.getElementById("date").value = Math.floor(result); 

Upvotes: 1

Related Questions