Apuranic
Apuranic

Reputation: 39

Form can't save when adding date format javascript using ajax

Before I add date using javascript, I can save my form. Here is the ajax code

function add_computer()
{
  save_method = 'add';
  $('#form')[0].reset();
  $('#modal_form').modal('show'); // show bootstrap modal 
  $('.modal-title').text('Add'); // Set Title to Bootstrap modal title
}

And, I add date using format javascript like the script below

function add_computer()
{
  save_method = 'add';
  $('#form')[0].reset(); // reset form on modals
  var dat = new Date();
  var day = dat.getDate();
  var month = ("0" + (dat.getMonth()+1)).slice(-2);
  var year = dat.getFullYear();

 document.getElementById("tanggal").value = day+"-"+month+"-"+year;
  $('#modal_form').modal('show'); // show bootstrap modal 
  $('.modal-title').text('Add'); // Set Title to Bootstrap modal title
}

I cannot save my form, and the error is

"TypeError: document.getElementById(...) is null"

But, the date can show in website.

Why the result become that? and How to solve this?

Upvotes: 1

Views: 86

Answers (2)

Kevin P
Kevin P

Reputation: 601

You can set the value in jQuery:

$('#tanggal').val(day+"-"+month+"-"+year);

Upvotes: 0

Anjana
Anjana

Reputation: 11

You have tried to modify the value of this element - document.getElementById("tanggal") Are you sure there is an element with id tanggal ?? Can you share your html, and the full console error, to help you further ?

Upvotes: 1

Related Questions