Solved

Javascript checkbox to enter date in form field

Posted on 2011-02-15
8
519 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
Industry Leaders: 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!

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
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…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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