1

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?

2
  • I am using oracle database and codeigniter. Commented Jul 12, 2017 at 2:04
  • Can you add your html markup ? The id if exists should not give you that error, may be you are running the script before the form loads, It would be more clearer if you add you html as well and indicate where you put your js Commented Oct 27, 2017 at 3:55

2 Answers 2

1

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 ?

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

2 Comments

<input type="text" id="tanggal" name="TGL_KELUHAN">
Please do not use answers to ask clarification questions.
0

You can set the value in jQuery:

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

5 Comments

not yet. Where i put that code? Ang what the function? Thanks for explanation
Replace document.getElementById("tanggal").value = day+"-"+month+"-"+year; with $('#tanggal').val(day+"-"+month+"-"+year); Also make sure that an element with the id of tanggal exists.
Here's an example of your code working in JSFiddle, you didn't provide any HTML files so I had to add the missing elements as I saw fit. jsfiddle.net/7kf7tdk5/3
<form action="#" id="form" class="form-horizontal"> <input type="text" id="tanggal" name="TGL_KELUHAN"> <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button> here is html code..
You need to create a save function. I updated JSFiddle to give you an idea of how to do this and get the form data. jsfiddle.net/7kf7tdk5/12

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.