JDV590
JDV590

Reputation: 651

display the check mark check / uncheck checkbox jquery

I am trying to fill a text box with the system date using jquery. the user will check and uncheck a checkbox to fill / unfill the txtbox with the date. This works correctly yet the checkbox does not visually check or uncheck while it performs these actions. how can i make the checkbox check and unckeck as the user clicks it? Here is the code:

EDIT: this is a asp.net checkbox I am using

 $('#ContentPlaceHolder1_FinishFillCHK').toggle(function () {
            var myDate = new Date();
            var prettyDate = (myDate.getMonth() + 1) + '.' + myDate.getDate() + '.' +
    myDate.getFullYear();
            $('#ContentPlaceHolder1_FinishDateSrvcTXT').val(prettyDate);
            $('#ContentPlaceHolder1_FinishFillCHK').attr('checked', true)
        }, function () {
            $('#ContentPlaceHolder1_FinishDateSrvcTXT').val('');
            $('#ContentPlaceHolder1_FinishFillCHK').attr('checked', false);
        });

Upvotes: 0

Views: 611

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

Try changing your code to

$('#<%=FinishFillCHK.ClientID%>') 

it may not make any difference in whats happening for you but is a much better way to get the ID of a control.

Upvotes: 0

Dennis
Dennis

Reputation: 32608

jQuery toggle will show/hide an element. Use click to provide a handler for the click event, then check to see what the user did:

 $('#ContentPlaceHolder1_FinishFillCHK').click(function () {
     if(this.checked) {
         var myDate = new Date();
         var prettyDate = (myDate.getMonth() + 1) + '.' + myDate.getDate() + '.' +
myDate.getFullYear();
         $('#ContentPlaceHolder1_FinishDateSrvcTXT').val(prettyDate);
     } else { //if not checked
         $('#ContentPlaceHolder1_FinishDateSrvcTXT').val('');
     }
});

Upvotes: 1

Related Questions