0
<div class="row">
    <div class="col-md-3" style="padding-top: 10px">
    <class="tg-83xr"> <select id="year" name="year">

    <script>
        var myDate = new Date();
        var year = myDate.getFullYear();

        for(var i = 1900; i < year+1; i++){
            document.write('<option  value="'+i+'">'+i+'</option>');}
    </script></select>
    </div>

    <div class="col-md-3" style="padding-top: 10px">    
        <input type="text" class="form-control" id="d000" enabled size = "30">      
    </div>

    <div class="col-md-3" style="padding-top: 10px">    
    <input type="text" class="form-control" id="d001" enabled size ="30" >      
    </div>
</div>

<script>
    $(document).ready(function(){
    $("#num").click(function () {
    $('#form').append("<div class='row'><div class='col-md-3' style='padding top: 10px'><select class=tg-83xr id='year' > </select></div><div class='col-md-3' style='padding-top: 10px'>   <input type='text' class='form-control' id='d000'>    </div><div class='col-md-3' style='padding-top: 10px'><input type='text' class='form-control' id='d001'></div></div>")});});
</script>

<div id="form">
</div>

<br>
<button id="num"> ADD </button> To add another click here

Here, i am not able to add options in the dropdown list which contains a list of years(1900-2016) in the .append() function.

2
  • Can you create a demo on fiddle? Commented Jun 6, 2016 at 13:45
  • Script tag is not allowed inside select element; get select element id and append options to it; Commented Jun 6, 2016 at 14:18

2 Answers 2

1

Better don't use document.write. You can use append() method to add the options to select.

<select id="year" name="year">
  </select>
<script>
var myDate = new Date();
var year = myDate.getFullYear();

for (var i = 1900; i < year + 1; i++) {
  $("#year").append('<option  value="' + i + '">' + i + '</option>');
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

1
var optionsString = "";

for( var i = 1900; i <= 2016; i++ ) // OR for( var i = 1900; i <= new Date().getFullYear(); i++ )
{
    optionsString += '<option value="'+i+'">'+i+'</option>';
}

 $('#form').append("<div class='row'><div class='col-md-3' style='padding top: 10px'><select class=tg-83xr id='year' >"+optionsString+"</select></div><div class='col-md-3' style='padding-top: 10px'>   <input type='text' class='form-control' id='d000'>    </div><div class='col-md-3' style='padding-top: 10px'><input type='text' class='form-control' id='d001'></div></div>")});});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.