1

I've made a custom select box that opens when a button is clicked which works fine. However, with multiple select boxes on the same page when one button is clicked all of the select boxes are opened up. Is there a way to do this without applying separate classes to each select box and multiple functions for each select box ?

Here's my HTML.

<div class="select-box-wrap">
    <select class="select-box" name="mbpanel_layout_section_options[site_layout]">
        <option value="1" <?php if(1 == $options['site_layout']) echo 'selected'; ?>>Left Sidebar</option>
        <option value="2" <?php if(2 == $options['site_layout']) echo 'selected'; ?>>Right Sidebar</option>
        <option value="3" <?php if(3 == $options['site_layout']) echo 'selected'; ?>>Three Column</option>
        <option value="4" <?php if(4 == $options['site_layout']) echo 'selected'; ?>>Full Width</option>
    </select>
    <input type="button" class="select-click"/>
</div>

<div class="select-box-wrap">
    <select class="select-box" name="mbpanel_layout_section_options[post_layout]">
        <option value="1" <?php if(1 == $options['post_layout']) echo 'selected'; ?>>Left Thumbnail</option>
        <option value="2" <?php if(2 == $options['post_layout']) echo 'selected'; ?>>Right Thumbnail</option>
        <option value="3" <?php if(3 == $options['post_layout']) echo 'selected'; ?>>Top Thumbnail</option>
    </select>
    <input type="button" class="select-click"/>
</div>

and here's the jQuery.

(function($){
   $(".select-click").click(function () {
       var size = $('.select-box option').size();

       if (size != $(".select-box").prop('size')) {
           $(".select-box").prop('size', size);
           $(".select-box").addClass("select-open");
       } else {
           $(".select-box").prop('size', 1);
           $(".select-box").removeClass("select-open");
       }
    })
})( jQuery );

and here's a JSFiddle

1 Answer 1

2

You need to use DOM traversal to find the .select-box related to the clicked button instead of using the class selector which affects all instances of the element. Try this:

(function($) {
    $(".select-click").click(function() {
        var $selectBox = $(this).prev('.select-box');
        var size = $selectBox.find('option').size();
        var sizeDifference = size != $selectBox.prop('size');
        $selectBox.prop('size', sizeDifference ? size : 1).toggleClass("select-open", sizeDifference);
    })
})(jQuery);

Working example

Note that I also changed the logic slightly with the use of a ternary expression to make the code shorter.

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

5 Comments

DOM traversal pairs your code to your html layout - if this is an issue (in most cases it's not) then you can "pair" your .select-click and .select-box using data attributes and find them that way. If this is not an issue, this is a fine answer.
Thank you. That was exactly what i needed. Perhaps you could help further though if possible. Would there be a way to close the box if an option is clicked of if anywhere else on the page is clicked like a normal select box ?
You just need to listen for a click outside of the select-box-wrap element, jsfiddle.net/descrs2p/4
Thank you that done the trick for clicking anywhere on the page but what about if clicked on one of the options ?
That's perfect! Thank you for all your help.

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.