mdrr5545
mdrr5545

Reputation: 29

Removing an option from a select with jQuery

What's wrong with this code? I want to delete an option [that is stored in a variable) from a select when the option is already selected. I receive this error from the console:

Uncaught Error: Syntax error, unrecognized expression: select.nuevaDescripcionProducto option[value=Aspiradora Industrial ]

this is what I have so far:

$(".formularioVenta").on("change", "select.nuevaDescripcionProducto", function() {

var nombreProducto = $(this).val();

$("select.nuevaDescripcionProducto option[value="+nombreProducto+"]").remove();

If anyone have any idea, please, let me know...

Upvotes: 0

Views: 37

Answers (1)

charlietfl
charlietfl

Reputation: 171690

You need extra quotes for the value since there is a space in the string

$("select.nuevaDescripcionProducto option[value='"+nombreProducto+"']").remove();
                                             // ^^ single quotes  ^^

Or as alternative using filter()

 $("select.nuevaDescripcionProducto").children().filter((_,op)=> op.value===$(this).val()).remove()

Upvotes: 2

Related Questions