add option value of the select box dynamically using javascript -



add option value of the select box dynamically using javascript -

i want populate year in select javascript. did next code.

<html> <head> <script type="text/javascript"> $(document).ready(function(){ var cur_year=new date().getfullyear(); var obj=document.getelementbyid("yr"); alert(obj); (var = 2000; <= 2020; i++) { opt = document.createelement("option"); opt.appendchild(document.createtextnode(value)); opt.selected = selected; opt.value = i; opt.text=i; obj.appendchild(opt); } }); </script> </head> <body> <select id="yr"> <option>year</option> </select> </body> </html>

i don't know wrong in this. want populate year , want select current year in select box when user opens browser. 1 can help? please!

$(document).ready( function() { var cur_year=new date().getfullyear(); var obj=document.getelementbyid("yr"); (var = 2000; <= 2020; i++) { opt = document.createelement("option"); opt.value = i; opt.text=i; obj.appendchild(opt); } document.getelementbyid("yr").value = cur_year; }); just utilize .value , assign cur_year it.

working fiddle

javascript

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -