2

I've a custom select option box but I've a bug.

The bug is now, how do I close the custom select options when clicked outside.

In the default select options when clicked outside then the select options hide away and then again when clicked select box again the select options appear.

I want the same to appear to my custom select options.

<style type='text/css'>
        div.selectBox
        {
            position:relative;
            display:inline-block;
            cursor:default;
            text-align:left;
            line-height:30px;
            clear:both;
            color:#888;
        }
        span.selected
        {
            width:167px;
            text-indent:20px;
            border:1px solid #ccc;
            border-right:none;
            border-top-left-radius:5px;
            border-bottom-left-radius:5px;
            background:#f6f6f6;
            overflow:hidden;
        }
        span.selectArrow
        {
            width:30px;
            border:1px solid #60abf8;
            border-top-right-radius:5px;
            border-bottom-right-radius:5px;
            text-align:center;
            font-size:20px;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -o-user-select: none;
            user-select: none;
            background:#4096ee;
        }

        span.selectArrow,span.selected
        {
            position:relative;
            float:left;
            height:30px;
            z-index:1;
        }

        div.selectOptions
        {
            position:absolute;
            top:28px;
            left:0;
            width:198px;
            border:1px solid #ccc;
            border-bottom-right-radius:5px;
            border-bottom-left-radius:5px;
            overflow:hidden;
            background:#f6f6f6;
            padding-top:2px;
            display:none;
        }

        span.selectOption
        {
            display:block;
            width:80%;
            line-height:20px;
            padding:5px 10%;
        }

        span.selectOption:hover
        {
            color:#f6f6f6;
            background:#4096ee; 
        }           
 </style>

  <div class='selectBox'>
        <span class='selected'></span>
        <span class='selectArrow'>&#9660</span>
        <div class="selectOptions" >
            <span class="selectOption" value="Option 1">Rob</span>
            <span class="selectOption" value="Option 2">Charles</span>
            <span class="selectOption" value="Option 3">Smith</span>
        </div>
    </div>


 <script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

 <script type='text/javascript'><!--
        $(document).ready(function() {
            enableSelectBoxes();
        });

        function enableSelectBoxes(){
            $('div.selectBox').each(function(){
                $(this).children('span.selected').html($(this).children('div.selectOptions').children('span.selectOption:first').html());
                $(this).attr('value',$(this).children('div.selectOptions').children('span.selectOption:first').attr('value'));

                $(this).children('span.selected,span.selectArrow').click(function(){
                    if($(this).parent().children('div.selectOptions').css('display') == 'none'){
                        $(this).parent().children('div.selectOptions').css('display','block');
                    }
                    else
                    {
                        $(this).parent().children('div.selectOptions').css('display','none');
                    }
                });

                $(this).find('span.selectOption').click(function(){
                    $(this).parent().css('display','none');
                    $(this).closest('div.selectBox').attr('value',$(this).attr('value'));
                    $(this).parent().siblings('span.selected').html($(this).html());
                });
            });             
        }//-->
    </script>
Firnas
  • 379
  • 2
  • 8
  • 24
  • possible duplicate of [Use jQuery to hide a DIV when the user clicks outside of it](http://stackoverflow.com/questions/1403615/use-jquery-to-hide-a-div-when-the-user-clicks-outside-of-it) – artm Apr 19 '15 at 05:04

3 Answers3

3

Working Fiddle

Use following

//My code

$(document).mouseup(function (e)
{
    var container = $(".selectBox");

    if (!container.is(e.target)
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        $(".selectOptions").hide();
    }
});

For more refer here

Community
  • 1
  • 1
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
2

Use overlay when the select-box is open. When clicked on overlay, close the select-box. Here is the changes made.

CSS:

.selectBox {
    z-index: 101;
    /* should be 1 greater than overlay's z-index */
}
.overlay {
    z-index: 100;
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    height: 100%;
    opacity: 0.5;
    background-color: black;
    display: none;
}

Javascript:

$('.overlay').bind('click', function() {
    $('.selectBox .selectOptions').slideUp();
    $(this).hide();
});

When the selectBox is opened, show overlay, and hide the overlay when the selectBox is closed.

opacity of the overlay is shown for the purpose of understanding how it works. You can set opacity to zero.

Demo

$(document).ready(enableSelectBoxes);

function enableSelectBoxes() {
    $('div.selectBox').each(function() {
      $(this).children('span.selected').html($(this).children('div.selectOptions').children('span.selectOption:first').html());
      $(this).attr('value', $(this).children('div.selectOptions').children('span.selectOption:first').attr('value'));

      $(this).children('span.selected,span.selectArrow').click(function() {
        if ($(this).parent().children('div.selectOptions').css('display') == 'none') {
          $(this).parent().children('div.selectOptions').css('display', 'block');
          $('.overlay').show();
        } else {
          $(this).parent().children('div.selectOptions').css('display', 'none');
        }
      });

      $(this).find('span.selectOption').click(function() {
        $('.selectBox .selectOptions').slideUp();
        $('.overlay').hide();
        $(this).parent().css('display', 'none');
        $(this).closest('div.selectBox').attr('value', $(this).attr('value'));
        $(this).parent().siblings('span.selected').html($(this).html());
      });
    });
  } //-->

$('.overlay').bind('click', function() {
  $('.selectBox .selectOptions').slideUp();
  $(this).hide();
});
div.selectBox {
  position: relative;
  display: inline-block;
  cursor: default;
  text-align: left;
  line-height: 30px;
  clear: both;
  color: #888;
  z-index: 101;
}
span.selected {
  width: 167px;
  text-indent: 20px;
  border: 1px solid #ccc;
  border-right: none;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  background: #f6f6f6;
  overflow: hidden;
}
span.selectArrow {
  width: 30px;
  border: 1px solid #60abf8;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
  text-align: center;
  font-size: 20px;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -o-user-select: none;
  user-select: none;
  background: #4096ee;
}
span.selectArrow,
span.selected {
  position: relative;
  float: left;
  height: 30px;
  z-index: 1;
}
div.selectOptions {
  position: absolute;
  top: 28px;
  left: 0;
  width: 198px;
  border: 1px solid #ccc;
  border-bottom-right-radius: 5px;
  border-bottom-left-radius: 5px;
  overflow: hidden;
  background: #f6f6f6;
  padding-top: 2px;
  display: none;
}
span.selectOption {
  display: block;
  width: 80%;
  line-height: 20px;
  padding: 5px 10%;
}
span.selectOption:hover {
  color: #f6f6f6;
  background: #4096ee;
}
.overlay {
  z-index: 100;
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  height: 100%;
  opacity: 0.5;
  background-color: black;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div class='selectBox'> <span class='selected'></span>
  <span class='selectArrow'>&#9660</span>

  <div class="selectOptions"> <span class="selectOption" value="Option 1">Rob</span>
    <span class="selectOption" value="Option 2">Charles</span>
    <span class="selectOption" value="Option 3">Smith</span>

  </div>
</div>
<div class="overlay"></div>

See fiddle

Sevle
  • 3,109
  • 2
  • 19
  • 31
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

If you can add focus to the object when its clicked, then you can have an event listener that listens to when it loses focus (ie: when you click outside the box) and in that event handler, just close it.

carinlynchin
  • 389
  • 1
  • 3
  • 13