Arjang Arjang - 1 year ago 93
Javascript Question

Add Remove element with Jquery using a refrence to html element

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.

Answer Source

wrap it in $():

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download