Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use a check box to fill in a Date in text field.

Status
Not open for further replies.

Katiris

Programmer
Feb 23, 2005
25
0
0
US
I'm in need of help as I'm not a javascript person. I need to be able to check a checkbox and fill in a text field with the ODBCDateTime. Can anyone help me? I'm sure that there is a script already that does this type of thing.
 
Are you using any server side scripts like Coldfusion or ASP? Or this is simply in JS?

If only JS, try:
Code:
<script>
function applyDate() {
	// SET THE VAR TO HOLD THE FORM
	var Gm = document.formname;
	// CREATE A NEW DATE
	var Gm_date = new Date();
	// GET THE MONTH
	var Gm_month = Gm_date.getMonth();
		Gm_month = Gm_month + 1;
	// GET THE DATE
	var Gm_day = Gm_date.getDate();
	// GET THE YEAR
	var Gm_year = Gm_date.getFullYear();
	
	//IF THE CHECKBOX IS CLICKED...
	if(Gm.Gm_test.checked == true) {
		// POPULATE THE TEXTAREA WITH THE DATE
		Gm.Gm_date.value = Gm_month +'/'+ Gm_day +'/'+ Gm_year;
		// MAKE IT READONLY SO THE USER CANNOT UPDATE/MODIFY THE DATE
		Gm.Gm_date.readOnly = true;
		return false;
	}
}
</script>

<table>
	<tr>
		<td>
			<form name="formname">
				<input name="Gm_test" type="checkbox" value="date" onClick="return applyDate();">
				&nbsp;
				<input type="text" name="Gm_date" value=""> 
			</form>
		</td>
	</tr>
</table>

Tested in IE, NS and FF

[sub]
____________________________________
Just Imagine.
[sub]
 
The script for GUJUModel works but displays more information than I want. I want date time only not the day etc.

THe script from falconseye - I can't get to display the createodbcdateTime - it literally outputs createodbcdateTime.

So I need just a bit more help on this if you can indulge me a little more.
 
Hello again. I've done more testing and the script GUJUModel doesn't work correctly. It doesn't leave the box checked. And in both cases I need it to be conditional

If checked display date if check removed(off) remove the date.

So if I can get falconseye to display the date It would be correct. Sorry for confusion
 
Katiris, you can always modify the code and get it to do what you want.

But in any case, this takes care of everything for you:
Code:
<script>
function applyDate() {
    // SET THE VAR TO HOLD THE FORM
    var Gm = document.formname;
    // CREATE A NEW DATE
    var Gm_date = new Date();
    // GET THE MONTH
    var Gm_month = Gm_date.getMonth();
        Gm_month = Gm_month + 1;
    // GET THE DATE
    var Gm_day = Gm_date.getDate();
    // GET THE YEAR
    var Gm_year = Gm_date.getFullYear();
    [COLOR=red]
    //IF THE CHECKBOX IS CLICKED...
    if(Gm.Gm_test.checked == true) {
        // POPULATE THE TEXTAREA WITH THE DATE
        Gm.Gm_date.value = Gm_month +'/'+ Gm_day +'/'+ Gm_year;
        // MAKE IT READONLY SO THE USER CANNOT UPDATE/MODIFY THE DATE
        Gm.Gm_date.readOnly = true;      
        [COLOR=blue][b]//return false;[/b][/color]
    }[/color][COLOR=green]
    //IF THE CHECKBOX IS UNCHECKED, THE FIELD IS NULL...
    if(Gm.Gm_test.checked == false) {
        // CLEAR THE TEXTAREA WITH THE DATE
        Gm.Gm_date.value = "";
        // MAKES READONLY FALSE SO THE USER CAN UPDATE/MODIFY THE DATE MANUALLY
        Gm.Gm_date.readOnly = false;
    }[/color]
}
</script>

The bold is commented out, leaving the checkbox in a checked state when checked, and unchecked state when checkbox unchecked.

The green makes it so when the checkbox is checked the textfield is empty allowing the user to enter in data.



[sub]
____________________________________
Just Imagine.
[sub]
 
Thank you. Like I said Javascript is not my area. But sometimes am able to modify to suit my needs. Again thank you for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top