Jump to content

AsmSelect: add text to first option (Please select...)


tpr
 Share

Recommended Posts

I found no way to add text "Please select..." as the first item to an AsmSelect field. Using PHP I could add a disabled option but an empty first item is always prepended to the options list.

I also checked the JavaScript options but found no way to do this.

Is there something I overlooked? I can add this feature with a few lines of extra JS but it would be better if there were an "official" way.

Link to comment
Share on other sites

I solved it this way - a bit hacky but as it's only a cosmetic issue, it will do.

  • In the module I needed this feature I added a "data-asm-placeholder" to the asmSelect field
  • in module JS I added a $(function () { ... } inside document.ready() where I set the asmSelect field's first option text to the value set in data-asm-placeholder

post-3156-0-96826300-1445460453_thumb.pn

$(document).ready(function () {

    // other code here

    $(function () {
        $('select[data-asm-placeholder!=""][data-asm-placeholder]').each(function () {

            var placeholder = $(this).data('asmPlaceholder');

            if (placeholder) {
                $(this).parent().find('.asmSelect').find('option:first').attr('selected', true).attr('disabled', true).text(placeholder);
            }
        });
    });
});
  • Like 1
Link to comment
Share on other sites

For the record, just edited the snippet above and added "[data-asm-placeholder]".

The selector now says "If attr must exist & has to have some value...", so selects without the custom "data-asm-placeholder" will be skipped.

See:

http://stackoverflow.com/questions/10641258/jquery-select-data-attributes-that-arent-empty#answer-23944081

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...