-1

i want to set date with this format 2022-09-22T16:03 in input datetime-local when i click button Enregister but not work, i try this post 1link but not work also this link 2link not work.

     <link rel="icon" 
      type="image/png" 
      href="https://findicons.com/files/icons/2805/squareplex/512/google_calendar.png">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">       
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <div class="container main">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label for="exampleInputPassword1">Date de la tâche</label>
                    <input type="datetime-local" class="form-control" name="datee" id="date" required>
                </div>
            </div>
        </div>
        <div class="modal-footer ">
            <button  class="btn btn-success" id="add-event"><i class="fas fa-save"></i> Enregistrer </button>
        </div>          
        <script>
            $('#add-event').on("click", function(){
                 var date = "2022-09-22T16:03";
                 $("date").val(date);
                 console.log(date);
            });
        </script>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns"
        crossorigin="anonymous"></script>

2
  • The selector for ids requires the id to be prefixed with #. Commented Sep 28, 2022 at 15:43
  • 1
    Code works fine (jsfiddle) with a valid selector. Always test your selectors. Voted close as typo. Commented Sep 28, 2022 at 15:43

1 Answer 1

0

Change your selector, from $("date").val(date); to $("#date").val(date);

<link rel="icon" type="image/png" href="https://findicons.com/files/icons/2805/squareplex/512/google_calendar.png">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<div class="container main">
  <div class="row">
    <div class="col-sm-6">
      <div class="form-group">
        <label for="exampleInputPassword1">Date de la tâche</label>
        <input type="datetime-local" class="form-control" name="datee" id="date" required>
      </div>
    </div>
  </div>
  <div class="modal-footer ">
    <button class="btn btn-success" id="add-event"><i class="fas fa-save"></i> Enregistrer </button>
  </div>
  <script>
    $('#add-event').on("click", function() {
      var date = "2022-09-22T16:03";
      $("#date").val(date);
      console.log(date);
    });
  </script>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.