Jqueryui autocomplete does not work inside a Bootstrap model - html

I'm trying to use JqueryUI autocomplete inside Bootstrap modal, but it doesn't work:
<!-- This one Works -->
<div class="ui-widget">
<label for="locationSelector">City: </label>
<input id="locationSelector">
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#citySelectionModal">
Launch demo modal
</button>
<!-- Modal (the autocomplete does not work here) -->
<div class="modal fade" id="citySelectionModal" tabindex="-1" role="dialog" aria-labelledby="citySelectionModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="citySelectionModalLabel">Select city</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="ui-widget">
<label for="locationSelector">City: </label>
<input id="locationSelector">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-danger">Clear</button>
</div>
</div>
</div>
</div>
Js code:
$(function() {
var locationList = ['London', 'Bilbao', 'Paris', 'Madrid', 'Moscow', 'Berlin'];
$("#locationSelector").autocomplete({
source: locationList
});
});
I recreated an example in jsfiddle: Link
After some research, I think it is related to the modal contents being hidden, but I don't know how to solve it. Any suggestion?

First of all you should edit you css and add your autocomplete higher z-index because bootstrap .modal class has z-index: 1050; so that you could even see the autocomplete any higher number than 1050 should do it
.ui-autocomplete {
z-index: 1060;
}
Secondly you can't use same IDs in the same page. One way to fix it would be to change
id -> class
Change your HTML input fields from id="locationSelector" to class="locationSelector" and change JS jQuery selector from $("#locationSelector") to $(".locationSelector").
I also provided a working example: https://jsfiddle.net/dh4e49z7/33/

your input field have same ids (locationSelector)
<div class="ui-widget">
<label for="locationSelector">City: </label>
<input id="locationSelector">
</div>
and
<div class="modal-body">
<div class="ui-widget">
<label for="locationSelector">City: </label>
<input id="locationSelector">
</div>
</div>
if same page has multiple same ids on different input fields it will not work.

Add CSS below:
.ui-autocomplete {
z-index: 4000 !important;
}

Related

"data-bs-dismiss" not working in Bootstrap 5

I have two modals. I'm trying to make the "Novo Avatar" button close its modal, which is #modal_debug, and then open the modal #modal_newAvatar. The problem is it doesn't close the modal, and instead just opens the new one on top.
I've even tried to copy paste this example from the BS docs, which supposedly does the same I'm trying to achieve
https://getbootstrap.com/docs/5.0/components/modal/#toggle-between-modals
But the exact same happens. Is it something wrong with my version of bootstrap? I'm using 5.1.0.
<!-- Modal "Painel de Debug" -->
<div class="modal fade" id="modal_debug" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2>Painel de Debug</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<button type="button" class="btn btn-primary" data-bs-target="#modal_newAvatar" data-bs-toggle="modal" data-bs-dismiss="modal">Novo Avatar</button>
<button type="button" class="btn btn-primary" data-bs-target="#modal_selAvatar" data-bs-toggle="modal" data-bs-dismiss="modal">Selecionar Avatar</button>
<button type="button" class="btn btn-primary" data-bs-target="#modal_delAvatar" data-bs-toggle="modal" data-bs-dismiss="modal">Remover Avatar</button>
</div>
</div>
</div>
</div>
<!-- Modal "Novo Avatar" -->
<div class="modal fade" id="modal_newAvatar" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5>Novo Avatar</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form id="form_newAvatar" autocomplete="off">
<label for="input_newAvatar_sceneKey">Cena de Jogo</label><br>
<input type="text" id="input_newAvatar_sceneKey" name="sceneKey" value="main-room" required><br><br>
<label for="input_newAvatar_name">Nome</label><br>
<input type="text" id="input_newAvatar_name" name="name" required><br><br>
<label for="input_newAvatar_x">x</label><br>
<input type="number" id="input_newAvatar_x" name="x" required><br><br>
<label for="input_newAvatar_y">y</label><br>
<input type="number" id="input_newAvatar_y" name="y" required>
</form>
</div>
<div class="modal-footer">
<button type="submit" form="form_newAvatar" class="btn btn-primary">Confirmar</button>
</div>
</div>
</div>
</div>
Had the same problem as you, use this solution: https://stackoverflow.com/a/58553800
It involves JavaScript, modify it to suit your needs.
<script>
//When clicking the first button this function
//hides the first modal and then shows the second modal
$(function () {
$('#firstModalButtonID').on('click', function () {
$('#firstModalID').modal('hide');
$('#secondModalID').modal('show');
})
})
//When clicking the second button this function
//hides the second modal and then shows the first modal
$(function () {
$('#secondModalButtonID').on('click', function () {
$('#secondModalID').modal('hide');
$('#firstModalID').modal('show');
})
})

Point on an anchor on bootstrap modal opening

I would like to open a help modal, but depending on which link is clicked I need to focus on specific anchor like we do on a FAQ page.
On a page we will add #myAmazingTitle on href link (e.g FAQ for focusing the display on specific dom element.
<h1>My first title</h1>
<h1 id="myAmazingTitle">My Amazing Title</h1> <== Display will be focused on this title
....
I would like to have the same behavior with a bootstrap modal.
Is it possible and how?
This is Fiddle for this issue.
Use relatedTarget and HTML data-* attributes to specify target element selector (or anchor) and send this value to a handler of shown.bs.modal event. See example of usage below.
$('#exampleModal').on('shown.bs.modal', function(e) {
var recipient = $(e.relatedTarget).data('focus');
$(this).find( recipient ).focus();
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<span class="modal-title" id="exampleModalLabel">Registration form</span>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="EmailInput">Email address</label>
<input type="email" class="form-control" id="EmailInput" placeholder="Email">
</div>
<div class="form-group">
<label for="PassInput">Password</label>
<input type="password" class="form-control" id="PassInput" placeholder="Password">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Register</button>
</div>
</div>
</div>
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-focus="#EmailInput">Type Email</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-focus="#PassInput">Type Password</button>

Buttons coming while hover over an image

I want that when I move my cursor on an image, it should show 3 buttons coming and then when I click on any one of them, a modal should open. I am actually done with this, but the problem is when I click on any of the buttons appearing, a modal is reflected and as soon as I try to close that modal, those three options are shown on that image rather dis-appearing. Please anyone help.
Here is the code:
<div class="row text-center">
<div class="col-sm-4">
<div class="thumbnail">
<div class="caption">
<button class="btn" data-toggle="modal" data-target="#myModal1a" style="color:#000">Add</button>
<button class="btn" data-toggle="modal" data-target="#myModal1b" style="color:#000">Delete</button>
<button class="btn" data-toggle="modal" data-target="#myModal1c" style="color:#000">Modify</button>
</div>
<img src="Department.png" style="position:fixed" alt="ALT NAME" width="250" height="60">
</div>
<h3 class="text" id="D1"><b>Department Options </b></h3>
</div>
</div>
<!-- Modal 1a -->
<div class="modal fade" id="myModal1a" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4><span class="glyphicon glyphicon-lock"></span> Create Department</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label for="psw">Department Name</label>
<input type="text" class="form-control" id="psw" placeholder="Provide a name">
</div>
<div class="form-group">
<label for="usrname">Department ID</label>
<input type="text" class="form-control" id="usrname" placeholder="Provide an ID">
</div>
<button type="submit" class="btn btn-block">Submit
<span class="glyphicon glyphicon-ok"></span>
</button>
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger btn-default pull-left" data-dismiss="modal">
<span class="glyphicon glyphicon-remove"></span> Cancel
</button>
<p>Need help?</p>
</div>
</div>
</div>
</div>
If you simply wanna hide those buttons whenever the modal is closed, then its a cakewalk if you use jQuery. You just need to use the hidden.bs.modal event of Bootstrap modals in the following way:
$('#myModal1a').on('hidden.bs.modal', function () {
$('.imgButton').each(function () {
$(this).hide();
})
})
Assuming that you provide a class called 'imgButton' to all the buttons :)

Passing parameter to bootstrap modal button

I have a delete link , for that I am using bootstrap modal window . Here window appearing fine When I click yes button of window the record should be delete. So I have to call servlet in yes button along with a parameter Id here I am able to call servlet but parameter is not passing. How can I pass parameter to yes button of modal window. Here is code for it.
The bootstrap modal where I am calling,
<td>Edit
</td>
<td> Delete
</td>
in the same jsp page the markup of modal is,
<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Delete Teacher Record</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
<a href="<%=request.getContextPath()%>/controller/TeacherManagementController?flowName=DELETE_TEACHER_INFO&teacherId=${teacherId.getTeacherId()}"
class="btn btn-primary">Yes</a>
</div>
</div>
</div>
</div>
The problem I am facing is the modal is appearing but record is not delete because of not able to pass the teacherId. Please any one help me in this.
I am using like this call function and assign value to hidden fields
Html:
<div id="add_button" >
<a data-toggle="modal" href="#myModal_new" onClick="pop_up('<?php echo $section_value['subject_name']; ?>','<?php echo $section_value['section_id']; ?>')">Add Subjects</a>
</div>
<script>
function pop_up(name,id){
var standard = document.getElementById("standard");
var standard_id = document.getElementById("standard_id");
var standard_id_new = document.getElementById("standard_id_new");
standard.value = name;
standard_id.value = id;
standard_id_new.value = id;
}
</script>
<div id="myModal_new" class="modal hide fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Create Sections</h3>
</div><br><br>
<div id="errorContainers">
<p>Please correct the errors and try again:</p>
<ul />
</div>
<div class="modal-body" style="margin-left: 17%;">
<!-- Modal Form Starts Here -->
<form name="modalForm" id="formss" method="post">
<input type="hidden" name="standard" id="standard">
<input type="hidden" name="standard_id" id="standard_id">
<input type="hidden" name="standard_id_new" id="standard_id_new">
<fieldset>
<div class="division_center"><div class="division_left"><label class="form-signin-signup" for="input1">Sections </label> </div>
<div class="division_right">
<input type="text" name="standard_new" id="standard_new"> </div></div>
</fieldset>
<!-- Modal Form Ends Here -->
</div>
<div class="modal-footer">
<a class="btn" data-dismiss="modal" >Close</a>
<input type="submit" name="newsubmit" class="btn btn-primary btn-large" value="Create">
</div>
</form>
</div>
</div>
</div>

How to center input in a Bootstrap 3 modal?

I'm currently switching over to Bootstrap 3 from bootstrap 2. How do you center the input field in Bootstrap 3? The text centers correctly. It used to work for BS2. I have tried moving the 'style='text-align:center' in div's but still no luck.
JSfiddle
Updated fiddle with answer
<div id="Email_Alert_Modal" class="modal show" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style="text-align: center">
<h3>Email Alert</h3>
</div>
<div class="modal-body" style="text-align: center">
<h4>Please enter email address that the alert will be sent to.</h4>
<label for="send_email"> </label> <input style="width:200px" type="email" class="form-control" name="email" id="send_email" placeholder="johndoe#yahoo.com" title="Input Email Address">
</div>
<div class="modal-footer">
<button class="btn btn btn-primary" onclick='Email_Alert()' >Send</button>
<button class="btn btn btn" data-dismiss="modal" >Cancel</button>
</div>
</div>
</div>
</div>
Set
margin: 0 auto;
for input element