0

I want to remove option from a selected dropdown, for this I've made removeValue() function but is not working as expected.

I debug the code and found that [value=removeid]. Var removeid is not working as expected here.

JavaScript function removeValue

function removeValue() {
  var removeid = $('#transaction_user_account_id').val() 
  alert(removeid); // I am getting my ID here
  $("#transaction_transfer_account_id option[value=removeid]").remove();
 };

If I give a constant value like value="21", then is working properly but when I assign my Var removeid to option[Value=removeid]. It stops working.

How can I make this work?

myst1c
  • 593
  • 3
  • 13
compsy
  • 233
  • 4
  • 12
  • Why do you expect a variable to be expanded inside a string? – Barmar Sep 26 '17 at 07:29
  • This link seems to be the answer of your question. [Clear all options from Select](https://stackoverflow.com/questions/3364493/how-do-i-clear-all-options-in-a-dropdown-box) Th – Night Coder Sep 26 '17 at 07:33

2 Answers2

1

That's because you're using removeid as string, not as a variable. Try this instead:

$("#transaction_transfer_account_id option[value=" + removeid + "]").remove();

Or you want to be all fancy-pants ES6 use a template literal:

$(`#transaction_transfer_account_id option[value="${removeid}"]`).remove();
Andy
  • 61,948
  • 13
  • 68
  • 95
0
$("#transaction_transfer_account_id option[value=removeid]").remove();

the above statement seems to be wrong.

'removeid' is a variable here, but in above statement its not been treated as same. it should be rewritten as

$("#transaction_transfer_account_id option[value=" + removeid + "]").remove();