Dropdown closes every click

Asked

Viewed 55 times

3

Good morning Guys,

I have a dropdown where I have several checkboxes contained in it. However when I click on any checkbox the dropdown selects only it and closes, I wanted to keep the dropdown open until I finish my choices.

Would that be possible? If so, how can it be done?

Dropdown:

$('#userList').append(
                '<div class="custom-control custom-checkbox ml-2">'+
                    '<input type="checkbox" class="custom-control-input" id="'+data[i].id+'">'+
                    '<label class="custom-control-label" for="'+data[i].id+'">'+data[i].nome+' '+data[i].sobrenome+'</label>'+
                '</div>'
                );

Div from the Dropdown:

<div  class='col-xs-12 col-sm-4 col-md-4 col-lg-2 dropdown'>
                    <button class="btn btn-info botao" type='button' id="user" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fas fa-users"></i></button>
                    <div class="dropdown-menu" aria-labelledby="user" id="userList">

                    </div>
                </div> 

2 answers

5


$(document).on('click', '#userList', function (e) {
    e.stopPropagation();
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.css">

<div class="row no-gutters">
    <div class='col-xs-12 col-sm-4 col-md-4 col-lg-2 dropdown'>
        <button class="btn btn-info botao" type='button' id="user" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-users"></i></button>
        <div class="dropdown-menu px-4 py-3" aria-labelledby="user" id="userList">
            <div class="form-group">
                <div class="row">
                    <div class="col-12">
                        <label class="custom-control custom-checkbox mb-0">
                            <input type="checkbox" class="custom-control-input">
                            <span class="custom-control-indicator"></span>
                            <span class="custom-control-description">Opção 1</span>
                        </label>
                    </div>
                    <div class="col-12">
                        <label class="custom-control custom-checkbox mb-0">
                            <input type="checkbox" class="custom-control-input">
                            <span class="custom-control-indicator"></span>
                            <span class="custom-control-description">Opção 2</span>
                        </label>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Show 8 more comments

-1

#userlist is a select?

If you give him the parameter Multiple, it Works ;)

example

<select multiple >
  <option value="" disabled selected>Choose your country</option>
  <option value="1">USA</option>
  <option value="2">Germany</option>
  <option value="3">France</option>
  <option value="4">Poland</option>
  <option value="5">Japan</option>
</select>

  • It’s a dropdown friend... the #userlist is a dropdown div, just pull it to be able to take the bd info to view with ajax.

  • dropdown is just the class you use.. you can put whatever you want inside.. https://answall.com/questions/215532/usar-bootstrap-para-multiselect-dropdown-com-checkbox

  • He’s using Dropdown of the Bootstrap, and not a common select.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.