Nitu Dhaka
Nitu Dhaka

Reputation: 1320

Dynamic length dropdown

I wanted to create the drop down list in the front end with dynamic length.

The length is not fixed here and the list will be numeric which will always start from 0 to up to length. I have no idea about jQuery so any help will be appreciated.

Upvotes: 0

Views: 68

Answers (2)

dreamweiver
dreamweiver

Reputation: 6002

Try this way

HTML CODE:

          <input id="cnt" type="text" name="count" size="10" />
          <input id="gen" type="button" name="generate" value="generate" />
          <select name="sel" id="ddlb"></select>

JQUERY CODE:

$('#gen').on('click', function () {
  if ($('#cnt').val().trim() !== "") {
    var index = 0;
    $('#ddlb').html("");
    while (index < $('#cnt').val()) {
        $('#ddlb').append($('<option>', {
            value: index,
            text: index
        }));
        index++;
    }
  }
});

LIVE DEMO:

http://jsfiddle.net/dreamweiver/aDqhH/6/

Happy Coding :)

Upvotes: 0

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

try like this

<script type="text/javascript">
    $(document).ready(function () {
        for (i = 0; i < 10; i++) {
            $("#mySelect").append($("<option>", { html: i }));
        }
    });

</script>
<body>
 <select id="mySelect"></select>
</body>

Upvotes: 1

Related Questions