[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Javascript checkbox to enter date in form field

Posted on 2011-02-15
8
Medium Priority
?
538 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

656 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