I have some issue in javascript variable load in to the bootstrap model input box:
setTimeout(function() {
swal({
title: "OverTime Status!",
text: "You Need to get Sub OT Approval " + data.value + " Hours to Time allocate in the department",
type: "warning",
confirmButtonText: "OK"
},
function(isConfirm) {
if (isConfirm) {
$('#hours_work').val(data.value); //data.value alert the correct value in here.but this value not load in to the bootstrap model text box
$('#overtime_requset').modal('show');
clear_field();
}
});
}, 1);
<div class="modal" id="overtime_requset">
<div class="modal-dialog ">
<form id="overtime_requset_form">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Sub OT Request</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<div class="input-daterange">
<input type="text" name="from_date" id="from_date" class="form-control" value="<?php echo $_GET[" month "]; ?>" readonly />
<span id="error_from_date" class="text-danger"></span>
<span id="error_future_from_date" class="text-danger text-center"></span>
</div>
</div>
<div class="form-group">
<label class="text-info">Tolal Number Of Employee <?php echo get_total_employee_count($connect,$_SESSION["dept_Id"])?></br>
<label>Add the addition number of OT hours requried</lable>
<input type="text" name="hours_work" id="hours_work" class="form-control" value=""/>
<label class="text-secondary">Approved OT hours : <?php echo GetApprovedOt($connect,$_SESSION["dept_Id"],$currentMonth)?></br></label><br>
<label class="text-secondary">Pendding OT hours : <?php echo GetPendingOt($connect,$_SESSION["dept_Id"],$currentMonth)?></label>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" name="get_approval" id="get_approval" class="btn btn-info btn-sm">Get Approval</button>
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
I have some issue in javascript variable load in to the bootstrap model input box:
setTimeout(function() {
swal({
title: "OverTime Status!",
text: "You Need to get Sub OT Approval " + data.value + " Hours to Time allocate in the department",
type: "warning",
confirmButtonText: "OK"
},
function(isConfirm) {
if (isConfirm) {
$('#hours_work').val(data.value); //data.value alert the correct value in here.but this value not load in to the bootstrap model text box
$('#overtime_requset').modal('show');
clear_field();
}
});
}, 1);
<div class="modal" id="overtime_requset">
<div class="modal-dialog ">
<form id="overtime_requset_form">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Sub OT Request</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<div class="input-daterange">
<input type="text" name="from_date" id="from_date" class="form-control" value="<?php echo $_GET[" month "]; ?>" readonly />
<span id="error_from_date" class="text-danger"></span>
<span id="error_future_from_date" class="text-danger text-center"></span>
</div>
</div>
<div class="form-group">
<label class="text-info">Tolal Number Of Employee <?php echo get_total_employee_count($connect,$_SESSION["dept_Id"])?></br>
<label>Add the addition number of OT hours requried</lable>
<input type="text" name="hours_work" id="hours_work" class="form-control" value=""/>
<label class="text-secondary">Approved OT hours : <?php echo GetApprovedOt($connect,$_SESSION["dept_Id"],$currentMonth)?></br></label><br>
<label class="text-secondary">Pendding OT hours : <?php echo GetPendingOt($connect,$_SESSION["dept_Id"],$currentMonth)?></label>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" name="get_approval" id="get_approval" class="btn btn-info btn-sm">Get Approval</button>
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
data.value alert the correct value in here. But this value not load in to the bootstrap model text box. What is my mistake? How do I fix it? (only problem is pass JavaScript value not load in to the in side the text field. It can print as HTML out in to the div tag )
Update i used following cdn sweetAlert boostrap
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.css" />
Share
Improve this question
edited Dec 5, 2019 at 11:22
Krishna Savani
9624 silver badges10 bronze badges
asked Nov 26, 2019 at 9:45
Code KrisCode Kris
4474 silver badges20 bronze badges
12
- Try setText() function – Ahamed Safnaj Commented Nov 26, 2019 at 9:51
- not work bro .i think problem is something else – Code Kris Commented Nov 26, 2019 at 10:33
- i think you haven't linked the JS file. write an alert function and make sure you have linked it.. – Ahamed Safnaj Commented Nov 26, 2019 at 10:35
- alert(data.value); show the correct value – Code Kris Commented Nov 26, 2019 at 10:37
- 1 i use latest cdn 'sweetalert' – Code Kris Commented Dec 4, 2019 at 8:57
9 Answers
Reset to default 5 +50your posted code is bit lacking and has some errors, anyways i tried to replicate your problem and i think i may have gotten it working. Take a look at below jsfiddle snippet and see if you can implement in your code to get it working.
CHECK OUT THIS LINK FIRST: https://jsfiddle.net/b1nary/je60gxv8/2/
UPDATED WITH YOUR SWEETALERT: https://jsfiddle.net/b1nary/je60gxv8/11/
setTimeout(function() {
swal({
title: "OverTime Status!",
text: "You Need to get Sub OT Approval " + data_val + " Hours to Time allocate",
type: "warning",
showCancelButton: true,
confirmButtonText: "Yes, do it",
closeOnConfirm: false,
html: false
}, function(){
swal("Deleted!",
"Poof! Your number has been added to input box!",
"success");
$('#hours_work').val(data_val);
$('#overtime_requset').modal('show');
//clear fields
});
}, 1000 );
Try this. I think function(isConfirm)
is problem.
You need use then() function
swal({
title: "OverTime Status!",
text: "You Need to get Sub OT Approval " + data.value + " Hours to Time allocate in the department",
type: "warning",
showCancelButton: false
}).then(function (result) {
$('#hours_work').val(data.value);
$('#overtime_requset').modal('show');
});
For form input fields you need to use val()
instead of text()
.
Example: $('#hours_work').val(data.value);
Please check you html syntax
old
<input type="text" name="hours_work" id="hours_work" class="form-control value=""/>
new
<input type="text" name="hours_work" id="hours_work" class="form-control" value=""/>
and check with one by one
$('#hours_work').val(data.value);
$('#hours_work').text(data.value);
Your problem can be resolved if you simply use delegates.
Like instead of direct use $('#hours_work').val(data.value);
try with any parent refrence . of your document such as body. so $('#hours_work',$('body')).val(data.value);
You can use any parent reference of your modal div. in which your modal code is placed instead of the body. parent div of this div .
It's because your dom is not aware of your model elements if it's dynamically loaded.
Let me know if it's worked
You are facing this issue because you are trying to add value to a field #hours_work
, which has not yet rendered in DOM, as $('#overtime_requset').modal('show');
is rendering after assigning value to $('#hours_work').val(data.value);
and input field is a part of Model, so you just need to add model first and then assign value to input field:
setTimeout(function() {
swal({
title: "OverTime Status!",
text: "You Need to get Sub OT Approval " + data.value + " Hours to Time
allocate in the department",
type: "warning",
confirmButtonText: "OK"
},
function(isConfirm) {
if (isConfirm) {
$('#overtime_requset').modal('show');
$('#hours_work').val(data.value);
}
});
}, 1);
I have worked on Your Requirements and is working for me please run below code as locally:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script>
setTimeout(function() {
var myArray = {id1: 100, id2: 200};
swal({
title: "OverTime Status!",
text: "You Need to get Sub OT Approval "+ myArray.id1+ " Hours to Time allocate in the department",
type: "warning",
confirmButtonText: "OK"
},
function(isConfirm) {
if (isConfirm) {
$('#hours_work').val(myArray.id1); //data.value alert the correct value in here.but this value not load in to the bootstrap model text box
$('#overtime_requset').modal('show');
}
else{
console.log("test");
}
});
}, 1);
</script>
<div class="modal" id="overtime_requset">
<div class="modal-dialog ">
<form id="overtime_requset_form">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Sub OT Request</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<div class="input-daterange">
<input type="text" name="from_date" id="from_date" class="form-control" value="" readonly />
<span id="error_from_date" class="text-danger"></span>
<span id="error_future_from_date" class="text-danger text-center"></span>
</div>
</div>
<div class="form-group">
<label class="text-info">Tolal Number Of Employee </br>
<label>Add the addition number of OT hours requried</lable>
<input type="text" name="hours_work" id="hours_work" class="form-control" value=""/>
<label class="text-secondary">Approved OT hours : </br></label><br>
<label class="text-secondary">Pendding OT hours :</label>
</div>
</div>
<div class="modal-footer">
<button type="button" name="get_approval" id="get_approval" class="btn btn-info btn-sm">Get Approval</button>
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
Please write this javascript under bootstrap modal
<div class="modal" id="overtime_requset">
....
<script>
setTimeout(function () {
swal({
title: "OverTime Status!",
text: "You Need to get Sub OT Approval " + data.value + " Hours to Time allocate in the department",
type: "warning",
confirmButtonText: "OK"
},
function(isConfirm){
if (isConfirm) {
$('#hours_work').text(data.value);//data.value alert the correct value in here.but this value not load in to the bootstrap model text box
$('#overtime_requset').modal('show');
clear_field();
}
}); }, 1);
</script>
</div>
If you defined the data
JavaScript object and the clear_field
function and, called the JQuery and Bootstrap libraries. There wouldn't be no issues with your code.
Check this JSFiddle page: https://jsfiddle.net/1x6t3ajp