0

Can any one tell me the theat how can i set the value to the DIV TAG DYNAMICALLY USING javascript

<div class="sD" style="width:55px;" id="CYearHold">
    Here i have to display Current year
</div>      
<select id="CYear" class="inputControl simple" style="width:55px;" name="currentYear" onChange="setYear1(this);">
</select>
user1550575
  • 55
  • 2
  • 7
  • 1
    possible duplicate of [Replace text inside a DIV element](http://stackoverflow.com/questions/121817/replace-text-inside-a-div-element) – jbabey Aug 07 '12 at 13:04
  • We're not going to just 'give' you the code. The code is amazingly easy to create. Are you here to LEARN or to GET? If you want to learn, what have you tried so far? We could give you a little push in the right direction so you'll be able to do it yourself –  Aug 07 '12 at 13:04

2 Answers2

2

You can use innerHTML:

document.getElementById('CYearHold').innerHTML = 'something';

If you want to put the selected year in the div, you can do this:

var e = document.getElementById("CYear");
var strYear = e.options[e.selectedIndex].value;
document.getElementById('CYearHold').innerHTML = strYear;
psx
  • 4,040
  • 6
  • 30
  • 59
  • There i have to Kept present year so can i write like this var d = new Date(); document.getElementById('cYearHold').innerHTML = 'd.getFullYear()'; – user1550575 Aug 07 '12 at 13:09
  • You almost have it, just remove the single quotes round the d.getFullYear() and that should work. The quotes mean a literal, which means it would actually just show the text you typed in there. – psx Aug 07 '12 at 13:10
  • What is it showing?. Make sure you are typing CYearHold, not cYearHold. It is case sensitive. – psx Aug 07 '12 at 13:32
0

get the current year

select = document.getElementById("CYear");
year = select.options[select.selectedIndex].innerHTML;

set it in your div

document.getElementById("div.sd").innerHTML=year;
guillaume
  • 6,477
  • 5
  • 17
  • 15