Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

onChange event using javascript

Posted on 2008-06-24
4
Medium Priority
?
1,198 Views
Last Modified: 2008-11-27
Hello,  I'm trying to create a simple onChange event using javascript which takes a custom checkbox (example a sales step in an opportunity) and when checked I want it to populate  a completed date field automatically.  We currently have 4 steps in our sales process so we would like to track each step as people mature an opportunity.  Having troubles accomplishing this, have good programming knowledge but not much javascript.  Thanks.
0
Comment
Question by:ohmErnie
2 Comments
 
LVL 2

Accepted Solution

by:
mriz81 earned 252 total points
ID: 21859459
I did not get your requirement clearly. But I think you want to perform something ( populate date ) when a checkbox is checked on unchecked .. am I right?
If yes .. then you need to use onClick event instead of onChange event.
For Example
<input type="checkbox" name="chk1" value="1" onclick="alert('changed');" />

Open in new window

0
 
LVL 4

Assisted Solution

by:msfletch
msfletch earned 248 total points
ID: 21860829
mriz 81 is correct, you do need onClick instead of onChange.

I beleive the attached code does what you want. I tried to annotate it for clarity, so you should be able to add onto it and modify it according to your needs.

Basically, it sets or clears the date depending on whether or not the checkbox has been checked or unchecked. It also supports multiple checkboxes and date fields. Just change the date fields from "text" to "hidden" if you don't want them displayed.

Good luck.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
	<title>Untitled</title>
 
	<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
	<!--
	function setDate(whichCheckbox,whichDateField) {
	
                  // check if checkbox checked or unchecked
		if (document.getElementById(whichCheckbox).checked == true) {
	
			// create new date object
			var now = new Date();
		
			// parse elements of date object for display ... use the ones you want
			// var hour = now.getHours();
			// var minute = now.getMinutes();
			// var second = now.getSeconds();
			var monthnumber = now.getMonth();
			var monthday = now.getDate();
			var year = now.getYear();
		
			// combine elemets for desired format
			var entryDate = monthnumber + '/' + monthday + '/' + year;
		
			// enter date in field
			document.getElementById(whichDateField).value = entryDate;
		
		} else {
		
			// remove date from text field
			document.getElementById(whichDateField).value = '';
		}
					
	}
	//-->
	</SCRIPT>
 
</head>
 
<body>
 
<form name="form" method="post" action="somepage.html">
 
	<input type="checkbox" name="chk1" id="chk1" value="1" onclick="setDate('chk1','stepOneDate')" />
	<input type="text" name="stepOneDate" id="stepOneDate" value="" />
	<br />
	<input type="checkbox" name="chk2" id="chk2" value="1" onclick="setDate('chk2','stepTwoDate')" />
	<input type="text" name="stepTwoDate" id="stepTwoDate" value="" />
	
</form>
 
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

577 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