jQuery-UI autocomplete does not work on Bootstrap 4 modal

8.6k views Asked by At

I'm stucked with an issue in bootstrap 4 modal, I'm trying to use jQuery-UI's autocomplete function on an input but it doesn't work, I've tried:

.ui-front {
    z-index:1001 !important;
}

The function works fine without modal, but when I put it inside a form on a modal, it stops working.

I tried all tips on this post Autocomplete issue into bootstrap modal but I didn't have any success.

here is the code:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Auto Complete</title>
    <link rel="stylesheet" href="css/jquery-ui.css">

    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
    <style>
        .ui-autocomplete {
        }
    </style>

</head>
<body>

<div class="container">
    <button class="btn btn-success" data-target="#modalOS" data-toggle="modal">MODAL</button>
<div class="modal fade " id="modalOS">
    <div class="modal-dialog " role="document">
        <div class="modal-content">
            <div class="modal-header ">
                <h5 class="modal-title" id="exampleModalLongTitle">MODAL</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="formCadOs">
                    <div class="form-row ">
            <div class="form-group  ">
                <label for="cliente">User</label>
                <input type="text" class="form-control" name="txt_Cliente" id="txt_Cliente " placeholder="Cliente">
            </div>
        </form>
        </div>
    </div>
</div>
</div>

</body>
</html>
<script src="js/jquery.js"></script>
<script src="js/jquery-ui.js"></script>
<script type="text/javascript">

    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    ];
    $( "#txt_Cliente" ).autocomplete({
        source: availableTags
    });


</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
1

There are 1 answers

1
Abid Nawaz On

Check here the working example.

$(document).ready(function() {
  var availableTags = [
    "ActionScript",
    "AppleScript",
    "Asp",
    "BASIC",
    "C",
    "C++",
    "Clojure",
    "COBOL",
    "ColdFusion",
    "Erlang",
    "Fortran",
    "Groovy",
    "Haskell",
    "Java",
    "JavaScript",
    "Lisp",
    "Perl",
    "PHP",
    "Python",
    "Ruby",
    "Scala",
    "Scheme"
  ];

  $('#modalOS').on('shown.bs.modal', function() {

    $(".autocomplete").autocomplete({
      source: availableTags
    });
  })
})
.ui-autocomplete {
  position: absolute;
  z-index: 99999 !important;
  cursor: default;
  padding: 0;
  margin-top: 2px;
  list-style: none;
  background-color: #ffffff;
  border: 1px solid #ccc -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
}

.ui-autocomplete>li {
  padding: 3px 20px;
}

.ui-autocomplete>li.ui-state-focus {
  background-color: #DDD;
}

.ui-helper-hidden-accessible {
  display: none;
}
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>


<div class="container">
  <button class="btn btn-success" data-target="#modalOS" data-toggle="modal">MODAL</button>
  <div class="modal fade " id="modalOS">
    <div class="modal-dialog " role="document">
      <div class="modal-content">
        <div class="modal-header ">
          <h5 class="modal-title" id="exampleModalLongTitle">MODAL</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <form id="formCadOs">
            <div class="form-row ">
              <div class="form-group  ">
                <label for="cliente">User</label>
                <input type="text" class="form-control autocomplete" name="txt_Cliente" id="txt_Cliente " placeholder="Cliente">
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>