Solved

Javascript checkbox to enter date in form field

Posted on 2011-02-15
8
506 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now