I am somewhat a beginner to jquery. I am having trouble creating a function to fill a textbox with the current date when a checkbox is clicked. I have the routine working on its own, but i would like to wrap it in a function so i can call it multiple times on the page. I have included the code here as well as a jsfiddle link
html:
<span id="ContentPlaceHolder1_Label8">Start Date:</span>
<input name="ctl00$ContentPlaceHolder1$startdateSrvcTXT" type="text" id="ContentPlaceHolder1_startdateSrvcTXT" disabled="disabled" class="aspNetDisabled" />
<span id="ContentPlaceHolder1_Label11">Check to Start</span>
<input id="ContentPlaceHolder1_StartFillCHK" type="checkbox" name="ctl00$ContentPlaceHolder1$StartFillCHK" />
javascript:
/*----This works------
$('#ContentPlaceHolder1_StartFillCHK').click(function () {
if (this.checked) {
var myDate = new Date();
var prettyDate = (myDate.getMonth() + 1) + '.' + myDate.getDate() + '.' +
myDate.getFullYear();
$('#ContentPlaceHolder1_startdateSrvcTXT').val(prettyDate);
} else { //if not checked
$('#ContentPlaceHolder1_startdateSrvcTXT').val('');
}
});
*/
//this doesnt
function fillclick(txtid,checkid){
$("'" + checkid + "'").click(function () {
if (this.checked) {
var myDate = new Date();
var prettyDate = (myDate.getMonth() + 1) + '.' + myDate.getDate() + '.' +
myDate.getFullYear();
$("'" + txtid + "'").val(prettyDate);
} else { //if not checked
$("'" + txtid + "'").val('');
}
});
}
fillclick("#ContentPlaceHolder1_startdateSrvcTXT","#ContentPlaceHolder1_StartFillCHK");
You’re querying by ID; you need to use
#selectors:and
Edit: You’re already using them. There’s no need to quote anything, they’re already strings:
and
.