Hkachhia
Hkachhia

Reputation: 4539

How to get options of drop down list using jquery

I have get options of drop down list using jquery for add and remove in dropdown list as per selection.

I have tried below code for get each option but it did not work for me. It's give object and I want html code like <option value="1">1</option>

<select id="drpdown" name="drpdown" multiple="multiple" size="15">
  <option data-parent="1" value="1">Test</option>                                       
</select>


 $('#drpdown').children().each(function() {
      if($(this).data('parent') != parent) 
      {
           values.push($(this));
           $(this).remove();
      } 
      else 
      {
         $(this).show();
      }
   });

Upvotes: 1

Views: 689

Answers (1)

97ldave
97ldave

Reputation: 5249

You can use the change method in jQuery to do your code when the value of the drop down changes.

To get the html you need use the prop method with the "outerHTML" property.

Something like this:

$("select").change(function() {
    var a = $(this).children(":selected").get(0);
    $("div").text($(a).prop("outerHTML"));
});

I have created an example fiddle

Upvotes: 2

Related Questions