0

I am trying to add an element to a dropDown and after selection remove it, by naively trying to have htmlElement refrence defines as such : (Of course this doeasnt work)

var selectAnOption = "<option value='' selected='selected'>Select One</option>";

So later

 var addSelectAnOption = function () { ('#ddb1').prepend(selectAnOption ); };

var removeSelectAnOption = function () { selectAnOption.remove(); };

I have also tried the following variation:

var selectAnOption;

 var addSelectAnOption = function () { selectAnOption = $('#ddb1').prepend("<option value='' selected='selected'>Select One</option>"); };

var removeSelectAnOption = function () { selectAnOption.remove(); };

But this also didn't work, as selectAnOption was set to the dropDownBox itself

Of course I could add the option with an id and then later use that to find it to remove it, but that seemed not too pretty.

1
  • 1
    Assuming only one option is ever selected at one time, you could use $('option[selected="selected"]').remove(); Commented Jul 28, 2016 at 1:18

1 Answer 1

1

wrap it in $():

var selectAnOption = $("<option value='' selected='selected'>Select One</option>");

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.