Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery remove selected option from this

first post here, I come in peace :) I've searched but can't quite find what I'm after.

I am trying to manipulate the selected option of a select box. Can someone please explain why this works:

$('#some_select_box').click(function() {   $('#some_select_box option:selected').remove(); }); 

but this doesn't:

$('#some_select_box').click(function() {   $('this option:selected').remove(); }); 

I just want to use "this" instead of spelling out the id of the select box - can someone point me in the right direction for the correct syntax? It's driving me mad because it looks like it should be really simple. And I'm sure it is to someone, but not me, cos its the end of the day and I'm brain-fried... Any pointers much appreciated.

Cheers

like image 840
odavy Avatar asked Mar 22 '10 17:03

odavy


People also ask

How remove selected attribute from option in jQuery?

If it is a multi-select and you need to remove the selected from all rows, you can use the following: $("#mySelect option:selected"). each(function () { $(this). removeAttr('selected'); });

How do I remove specific option from select?

The option to be removed is selected by getting the select box. The value to be removed is specified on the value selector (value='optionValue') on the select box. The remove() method is then used to remove this selected option. The find() method can be used to find the option in the value with the value selector.

How do you remove all options from select in jQuery except first?

Explanation: If we want to remove all items from dropdown except the first item then we can use $('#ddlItems option:not(:first)'). remove(); Here we have excluded first item from being deleted. If we want to remove all items from dropdown except the last item then we can use $('#ddlItems option:not(:last)').


2 Answers

this isn't a css selector. you can avoid spelling the id of this by passing it as a context:

$('option:selected', this).remove(); 

http://api.jquery.com/jQuery/

like image 196
Mathieu Avatar answered Sep 20 '22 13:09

Mathieu


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

Using the find method.

like image 33
Vincent Ramdhanie Avatar answered Sep 20 '22 13:09

Vincent Ramdhanie