[jquery] 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

This question is related to jquery select this option selected

The answer is


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/


$('#some_select_box option:selected').remove();


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

Using the find method.


This is a simpler one

$('#some_select_box').find('option:selected').remove().end();

This should do the trick:

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

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to select

Warning: Use the 'defaultValue' or 'value' props on <select> instead of setting 'selected' on <option> SQL query to check if a name begins and ends with a vowel Angular2 *ngFor in select list, set active based on string from object SQL: Two select statements in one query How to get selected value of a dropdown menu in ReactJS DATEDIFF function in Oracle How to filter an array of objects based on values in an inner array with jq? Select unique values with 'select' function in 'dplyr' library how to set select element as readonly ('disabled' doesnt pass select value on server) Trying to use INNER JOIN and GROUP BY SQL with SUM Function, Not Working

Examples related to this

this in equals method React: "this" is undefined inside a component function How to access the correct `this` inside a callback? jQuery $(this) keyword Difference between $(this) and event.target? 'this' vs $scope in AngularJS controllers Difference between getContext() , getApplicationContext() , getBaseContext() and "this" Use of "this" keyword in C++ What is context in _.each(list, iterator, [context])? What does 'var that = this;' mean in JavaScript?

Examples related to option

Get Selected value from dropdown using JavaScript html select option SELECTED How to check if an option is selected? JQuery refresh select box android: changing option menu items programmatically How to implement the --verbose or -v option into a script? set option "selected" attribute from dynamic created option How to preSelect an html dropdown list with php? jQuery remove selected option from this using href links inside <option> tag

Examples related to selected

UITableViewCell Selected Background Color on Multiple Selection How can I disable selected attribute from select2() dropdown Jquery? How to set a selected option of a dropdown list control using angular JS How to set the 'selected option' of a select dropdown list with jquery html select option SELECTED How to check if "Radiobutton" is checked? Programmatically select a row in JTable dropdownlist set selected value in MVC3 Razor UIButton: set image for selected-highlighted state jQuery remove selected option from this