4

Possible Duplicate:
How to populate the options of a select element in javascript

I would like to create a dynamic select tag and options tag in javascript. Any suggestions?

Community
  • 1
  • 1
  • 2
    Although you've been a member for 7 months, I suggest you review the [faq#questions]. More importantly, [*what have you tried*](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – zzzzBov Jul 18 '12 at 14:22

1 Answers1

11

try javascript

   var select = document.createElement( 'select' );
   var option;
   var inputdata = 123||456||789;

    inputdata.split( '||' ).forEach(function( item ) {

        option = document.createElement( 'option' );

        option.value = option.textContent =        item;

        select.appendChild( option );
    });

jquery

var inputdata = 123||456||789;
 var split = input.split('||');
 var select = $('<select name="options" id="options"></select>');
 $.each(split, function(index, value) {
   var option = $('<option></option>');
   option.attr('value', value);
   option.text(value);
   select.append(option);
 });
 $('#container).empty().append(select); 
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263