Solved

Javascript checkbox to enter date in form field

Posted on 2011-02-15
8
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

724 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