Search code examples
jquerydropdown

Bootstrap 4 - Prevent opening of modal when clicked on dropdown button


I am using BS 4 Modal and my dropdown buttons are placed inside the div which is responsible for modal opening on click:

The problem is I want to prevent the opening of the modal when I clicked on the dropdown button

Here is my js fiddle

$("#addEventsButton").on('click', function(event) {
    $('#exampleModalLong').modal('show');

})

Solution

  • Please check this code snippet.

    $("#addEventsButton").on('click', function(event) {
     $('#exampleModalLong').modal('show');
    })
    .adiv {
        background-color: #000;
        display: flex;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js" integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.15.0/umd/popper.min.js" integrity="sha256-fTuUgtT7O2rqoImwjrhDgbXTKUwyxxujIMRIK7TbuNU=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.js" integrity="sha256-pl1bSrtlqtN/MCyW8XUTYuJCKohp9/iJESVW1344SBM=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css" integrity="sha256-Nfu23DiRqsrx/6B6vsI0T9vEVKq1M6KgO8+TV363g3s=" crossorigin="anonymous" />
    <!-- Button trigger modal -->
    
    <div class="adiv">
    
    <p id="addEventsButton">
     Click me to open the modal
    </p>
        
    <div class="dropdown">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Dropdown button
      </button>
      <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
      </div>
    </div>
    
    </div>
    
    
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <input type="radio" class="new-control-input" name="marker" value="r1">
            
            <input type="radio" class="new-control-input" name="marker" value="r2">
            
            <input type="radio" class="new-control-input" name="marker" value="r3">
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" id="add-e" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

    The only change I made is, moved the id addEventsButton from parent div tag to p tag

        <p id="addEventsButton">
     Click me to open the modal
    </p>