maztt
maztt

Reputation: 12294

jquery select list remove

there are 2 multiple select list on my page , one is there with a seperate remove button . selecting an item there in the selected list is removing the item from the first select list also.how will i specify which list to remove item from in this code

$().ready(function() {    
   $('#remove').click(function() {    
       return !$('#FeatureList option:selected').remove();      
   });         
});

Upvotes: 1

Views: 1020

Answers (1)

Nick Craver
Nick Craver

Reputation: 630429

If the list is relative, like this:

<select>...options...</select>
<input type="button" class="remove" />

You can do it like this:

$(function() {
  $('.remove').click(function() {
    $(this).prev('select').find('option:selected').remove();
  });  
}); 

Currently your code has IDs, leading me to believe you're using the same ID multiple times...this is invalid HTML, for a list that may appear n number of times, you should use classes and find the list relative to the button. If that's not possible, each combination needs unique IDs or classes.

Also, try and refrain from using $().ready as it's deprecated in jQuery 1.4+, you should use $(document).ready(func); or the shorter version: $(func);.

Upvotes: 1

Related Questions