1

I want to add options to a drop down list dynamically as per the integer value of a variable

this is my html code of dropdown list

<select class="inputreq" id="qty" name="qty"><option value="1">1</option></select>

Like below i am getting integer value at run time

var optioneValue = <%=getCurrentAttribute('item','custitem_max_qty_limit')%>      

Suppose if optioneValue is 3 i want to display 3 options in dropdown list like this

<select class="inputreq" id="qty" name="qty">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

I have tried with javascript but its not working..I am new to this can we achieve this using jquery?

<script type="text/javascript">
var i = 1;
while ( i <= <%=getCurrentAttribute('item','custitem_max_qty_limit')%> ) {
var addSelectOption = document.getElementById('qty');
addSelectOption.options[i-1]= new Option(i,i);
i++; // Increment i
}
</script>
user1881845
  • 371
  • 2
  • 7
  • 16
  • See the answers of http://stackoverflow.com/questions/170986/what-is-the-best-way-to-add-options-to-a-select-from-an-array-with-jquery or http://stackoverflow.com/questions/1730360/how-to-add-option-to-select-list-in-jquery – Sheepy Dec 01 '14 at 07:27
  • hope this will help http://jsfiddle.net/3908050/trdo0z5v/3/ – kki3908050 Dec 01 '14 at 07:41

4 Answers4

0

You can try with this:

<script type="text/javascript">
    var i;
    for( i=1; i <= <%=getCurrentAttribute('item','custitem_max_qty_limit')%>; i++ ) {
        $('<option/>',{
              value:i,
              text:i
        }).appendTo("#qty");
    }
</script>

Demo

Jai
  • 74,255
  • 12
  • 74
  • 103
0

use lt() selector in jquery

var optioneValue = 3;  // example 
$("#qty option").hide();
$("#qty option:lt(" + optioneValue + ")").show();

DEMO

Balachandran
  • 9,567
  • 1
  • 16
  • 26
0

Try that code

<select class="inputreq" id="qty" name="qty">
<%=
    var i = 1;
    while ( i <= getCurrentAttribute('item','custitem_max_qty_limit')) {
        var addSelectOption = document.getElementById('qty');
        addSelectOption.options[i-1]= new Option(i,i);
        i++; // Increment i
    }
%>
</select>
Ankit Chugh
  • 198
  • 1
  • 11
0

You can try this.

<select class="inputreq" id="qty" name="qty">
<?php
     for($i=1;$i<=getCurrentAttribute('item','custitem_max_qty_limit');$i++)
     {
          echo '<option value="'.$i .'">'.$i.'</option>';
     }
?>
</select>
Ankur
  • 496
  • 1
  • 6
  • 11