Search code examples
jquerydrop-down-menuspecial-charactershtml-selectampersand

jQuery: Select dropdown by HTML


I've written a jQuery function that allows me to select an option from a dropdown using the option's HTML.

$.fn.select = function( option ) {
    $(selector +  ' option').each(function() {
        if ($(this).text() == option)
            $(selector).val($(this).val());
    });
    return this;
};

The function works perfectly fine, except with options that contain ampersands (&). In this case, it will fail to properly select the corresponding option.

Is there any way I can make this work?

I am aware that there already exists a select function for jQuery; however, it seems that my select function is able to overwrite the existing function.


Solution

  • Seems to be working right with &, Whats the problem ?

    Though replacing & with & would be better.