Solved

Javascript checkbox to enter date in form field

Posted on 2011-02-15
8
510 Views
Last Modified: 2012-05-11
Hi,
I'm trying my first bit of javascript to solve a simple problem.. I want a checkbox on a form to enter the current date in another field. It seems incredibly simple yet I cannot get it to work so obviously I'm missing something fundamental and it driving me crazy!

Any help appreciated, current code as shown (checkbox is called r1inv and date field is called r1idt) which seems to do nothing at all at the moment.

Thanks
function set_date(field,check)
{
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth()
var day = date.getDate()
if (check.value="on")
  {
  if (month < 10) {month = "0" + month}
  if (day < 10) {day = "0" + day}
  field.value = day + "/" + month + "/" + year
  }
else
  {
  field.value = ""
  }
}
</SCRIPT>



<input type="checkbox" name="r1inv" checked="checked" onChange="set_date('r1idt',this)">

Open in new window

0
Comment
Question by:nigelr99
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:Tomarse111
ID: 34895287
You were pretty much there:
<script type="text/javascript">

function set_date(field,check){

var field=document.getElementById(field)
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth()
var day = date.getDate()
if (check.checked==true)
  {
  if (month < 10) {month = "0" + month}
  if (day < 10) {day = "0" + day}
  field.value = day + "/" + month + "/" + year
  }
else
  {
  field.value = ""
  }
}
</SCRIPT>



<input type="checkbox" name="r1inv" onClick="set_date('r1idt',this)">

<input type="input" id="r1idt" />

Open in new window

0
 
LVL 15

Expert Comment

by:Tomarse111
ID: 34895297
All I have changed is the event to be fired "onClick". You were also simply passing through the field name or ID into your function. This needs to be changed into an actual DOM element, thus I added:

var field=document.getElementById(field)

The finally I changed your if statement to detect whether or not the checkbox has been checked. If so enter the date, if not, then clear the element of any value.
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34895381

Here's another option to try:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26821822.html</title>
<script src="http://code.jquery.com/jquery-1.4.3.min.js" language="javascript"></script>
<script src="http://www.stash.talktalk.net/JavaScript/DateExtension.js" language="javascript"></script>
<script language="JavaScript">

$(function() {

  $("#r1inv").click( loadDate );

});

function loadDate() {
  var today = new Date();
  var date = ( this.checked )? today.toString( "dd/MM/YYYY" ) : "";
  $("#r1idt").val( date );
}

</script>

</head>
<body>

<input id="r1inv" type="checkbox" /> 
<input id="r1idt" /> 

</body>
</html>

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:nigelr99
ID: 34895559
Excellent - overlooked fundamentals like comparing two vales! DOH.

This works fine in a test page but when I use it in a browser (ie8) I get the javascript error below relating to the field.value="" line

Uncaught TypeError: Cannot set property 'value' of null

Can I not clear the text field like this?
0
 
LVL 15

Expert Comment

by:Tomarse111
ID: 34895578
That is suggesting it cannot find the variable "field". Could you post your code please?
0
 

Author Comment

by:nigelr99
ID: 34895846
Hi, I've stripped it down to a bare page but it seems to work in ie and not chrome? Code attached or on http://nr99.net/test2.htm
Cheers.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript"> 
function set_date(field,check){
var field=document.getElementById(field)
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth()
var day = date.getDate()
if (check.checked==true)
  {
  if (month < 10) {month = "0" + month}
  if (day < 10) {day = "0" + day}
  field.value = day + "/" + month + "/" + year
  }
else
  {
  field.value = ""
  }
}
</SCRIPT>
</head>
<body>

<form>
<table>
<tr>
<td class="ctr"><input type="checkbox" name="r1inv"	 
   checked="checked" onClick="set_date('r1idt',this)"></td>
<td><input class="input" type="text" name="r1idt" maxlength="20" 
   value="01/03/2011"></td>
</tr>
</form>
</table>
</body>
</html>

Open in new window

0
 
LVL 15

Accepted Solution

by:
Tomarse111 earned 250 total points
ID: 34895876
Ok the code attached works fine in FF, Chrome and IE8:

You simply need to add an ID to the input box "r1idt"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript"> 
function set_date(field,check){
var field=document.getElementById(field)
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth()
var day = date.getDate()
if (check.checked==true)
  {
  if (month < 10) {month = "0" + month}
  if (day < 10) {day = "0" + day}
  field.value = day + "/" + month + "/" + year
  }
else
  {
  field.value = ""
  }
}
</SCRIPT>
</head>
<body>

<form>
<table>
<tr>
<td class="ctr"><input type="checkbox" name="r1inv"checked="checked" onClick="set_date('r1idt',this)"></td>
<td><input class="input" type="text" id="r1idt" maxlength="20" value="01/03/2011"></td>
</tr>
</form>
</table>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:nigelr99
ID: 34896032
Spot on! Thanks very much for your help - saved me another couple of hours chasing my tail (hours which I don't have!).
Cheers
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question