Reputation: 1320
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
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
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