Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load java script variable in to the bootstrap model text box as value

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">&times;</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" />
like image 920
Code Kris Avatar asked Nov 26 '19 09:11

Code Kris


4 Answers

your 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 );
like image 87
Galzor Avatar answered Oct 21 '22 09:10

Galzor


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'); 
    });
like image 43
BCM Avatar answered Oct 21 '22 09:10

BCM


For form input fields you need to use val() instead of text().

Example: $('#hours_work').val(data.value);

like image 37
CodyKL Avatar answered Oct 21 '22 10:10

CodyKL


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);
like image 31
Arshad Shaikh Avatar answered Oct 21 '22 08:10

Arshad Shaikh