-1
    // selectbox
    $(function() {
    function selectbox(widgetSelector, optionsContainerSelector, optionsSelector, selectedOptionSelector, selectedOptionClassName, selectorClassName) {
        var target = widgetSelector;


        function initialize() {
            var widget = $(this);
            var selectboxNamespace = widget.data('selectbox-ns');
            var options = optionsContainerSelector; // The container which contains the options 
            var option = optionsSelector; // The options nested directly inside the options container
            var selected = selectedOptionSelector; // This is the selector you will style, no visible at all times portion of the selectbox
            var selectedOption = selectedOptionClassName; // This class is mechanical but be used to allow the user to visually track the item which is selected once the placeholder pops back into the selected region 
            var selector = selectorClassName; // Set the selector switch to a local var for reuse
            function closeAllOptions(event) { // For blurring and defocus of any intances of  the selectbox widget
                // This portion is for reseting the placeholder or selection text (depending if an items has indeed been selected yet)
                $(options).hide(); // Hide all intances of the options container
                $(target).children().children(selected).each(function(){ // Iterate through each one 
                    $(this).html( $(this).parent().parent().children(options).children('.'+selectedOption).html() )
                    // Set each instance of the selected class innerHTML to that of the selectedOption class if one has been selected 
                });
            }
            function closeOptions(event) {
                $(options).hide();

                // When the an item is selected, we'll close all instances the options container
            }
            function injectPlaceholder() {
                // We will use this again below (2) times
                widget.children().children(selected).html(widget.children().children(selected).data('placeholder'));
            }
            injectPlaceholder(); // Ensure the initial content is where it should be on load
            function toggleOptions(event) {
                // this = .options
                if (widget.children(options).is(':visible')) { // If selectbox is opened
                    widget.children(options).hide(); // Hide the options based on where the function was envoked from
                    widget.children().children(selected).html(widget.children(options).children('.' + selectedOption).html())
                } else { // if selectbox is closed
                    $(options).hide() // Hide all options (all instances)
                    widget.children(options).show(); // Show this particular instance
                    injectPlaceholder(); // Another time where injectPlaceholder is envoked()
                }
                event.stopPropagation(); // Stop the event from bubbling up the DOM further
            }

            function selectOption(event) { // Selecting an option from the now visible options container
                // this = .option
                var reqOption = $(this).html(); // Store the option content that was selected
                var reqData = $(this).attr('data-value'); // Store the value held by the option selected
                widget.children(options).children(option).removeClass(selectedOption) // This class is mechanical but be used to allow the user to visually track the item which is selected once the placeholder pops back into the selected region 
                $(this).addClass(selectedOption); // for tracking purposes
                widget.children().children(selected).html(reqOption) // Insert the selected options data into the 'selected'/selection area
                widget.children().children(selected).attr('data-value', reqData) // Append a data-attribute matching the value of the option selected
                $('.test-box').append(selectboxNamespace + ' was selected and ' + 'reports its selected value: ' + reqData + ',' + ' because the user selected the option reading: ' + '"' + reqOption + '"<br />');
                event.stopPropagation(); // Stop the event from bubbling up the DOM
                 closeOptions(); // Close options after an item is selected and the rest is done...
            }
            // Bind our event handlers
            $(this).on('click', option, selectOption) // If an option is clicked -> selectOption()
            $(this).on('click', toggleOptions) // If the options container is clicked -> toggleOption()
            $(this).on('click', selector, toggleOptions) // If the selector element is clicked -> toggleOptions()
            $('html').on('click', closeAllOptions) // If the  widget is blurred/defocused -> closeAllOptions
        }

        $(target).each(initialize) // Apply this functionality to each instance of the select box
    }
    window.selectbox = selectbox('.selectbox', '.options', '.option', '.selected', 'selected-option', '.selector');
    // API : widgetSelector, optionsContainerSelector, optionsSelector, selectedOptionSelector, selectedOptionClassName, selectorClassName ) The last param must be a class name (any classname)
    });

Here are the widgets requirements which I managed to meet:

Must retain placeholder if nothing is selected through all states (click and then click the selector not to select an actual option, and the placeholder content should stay)

When the options menu is open, it should show the placeholder instruction as the selected option text, however once the menu is contracted or toggled, it should show the selected text (if there is any)

The selectbox must close on blur, and also on selection of another instance of the widget

Obv it must grab a value and identifier for the selected option, and pair that with an identifier that lets the app know which box the data and value came from

PROBLEM:

With JQuery 1.7 loaded there is a bug when you attempt to toggle the selectbox without actually selecting an item, the innerHTML of the selection display reads nothing, instead of the desired placeholder content being re-added to the selection display as desired (the logic is there and working error free, please test it for yourself)

Also, when the body is clicked, all menu options should close, and if the widget/widgets have no option selected then the selected display area should again be injected with the placeholder content, if there has been an item selected in that instance then it should retain that instead.

Thanks for the help, Nicholas

  • 2
    It works on Chrome Versión 36.0.1985.125 m.... – Hackerman Aug 04 '14 at 18:30
  • Why does it stop working? Have you traced the error yet? – Kevin B Aug 04 '14 at 18:30
  • 1
    Bigger question is why do you need to use 1.7.1? Are you stuck using drupal or something? – Kai Qing Aug 04 '14 at 18:32
  • Works on Chrome 36.0.1985.125, Firefox 30.0, and Safari 7.0.5 (9537.77.4) on OS X Mavericks. – derekaug Aug 04 '14 at 18:33
  • No it doesn't! Stop taking away my points! 1) Load page 2) Click widget to open position 3) Click the placeholder selector image without selecting an underneath item and let the bug begin! – NicholasAbrams Aug 04 '14 at 18:51
  • 1.7 is the existing lib used on an website which is being expanded. I only wrote maybe 4 or 5 widgets so to replace the library because of this one thing goes against what I stand for which is finding out what caused the problem in the 1st place (with the help of SO of course haha!) – NicholasAbrams Aug 04 '14 at 18:55
  • No once again it really doesnt work in any browser unless jq 1.11.1 is loaded. Click the placeholder select button on the right of the placeholder selection, once the options below are displayed click it again without selecting an item. This will cause the placeholder text to vanish for some reason. Come on so far im the only right one here and i lost 2 points... sheeesh! I am on Chrome Version 36.0.1985.125 m. – NicholasAbrams Aug 04 '14 at 19:21

1 Answers1

0

http://codepen.io/anon/pen/koeCc

Here is the same code with one small change to the way that the .selected class was targeted (was being targeted directly instead of using .each($(this... ) I dont know why the bug worked with 1.11.1 but not with 1.7.1 but now it works for both!

line 25:  .html( $('.selected-option').html() )

is now diff by:

line 25:        .html( $(this).children(selected).html() )
  • It didn't, I rewrote the script as perfectly as I could http://codepen.io/anon/pen/smegx, in order to meet requirements however it will not work with jquery 1.7. I am already upgrading the jquery being used for this widget however I wish I knew the issue because that is the only way to learn! – NicholasAbrams Aug 05 '14 at 16:20
  • Then why did you post it as an answer? – Artjom B. Aug 05 '14 at 16:26
  • Because it appeared to be working, OP error. Any ideas? I am completely stumped! – NicholasAbrams Aug 05 '14 at 16:41