views:

2200

answers:

3

I'm cleaning up a simple form that has a Start Date textbox and an End Date textbox. I want to add a checkbox in between these fields that the user can check if the End Date is the same as the Start Date, so when they check it, the Start Date input value (e.g., 04/01/09) will automagically appear in the End Date textbox, so they don't have to type in the same date twice. Does that make sense?

BTW, I'm using the sexy jquery datepicker UI, and it's sweet, but I just can't figure out the above problem.

I know there's a simple solution (event handler?) but I'm stumped.

+4  A: 

Try this code:

$("#checkboxId").click(copyDate);

function copyDate()
{
   var start=$("#startDate").val();
   if (this.checked==true)
     $("#endDate").val(start);
}

Replace the 'id's with your own field ids.

Click Upvote
Thanks! You guys are fast!
Jesse
Np :) You can also upvote if you want
Click Upvote
What if someone tabs to the checkbox and hits space (i.e., checks it without clicking)?
Horace Loeb
Good point, he can add another event using $("#checkboxId").change(copyDate);
Click Upvote
Sorry to pick this up so late, but how exactly would I be able to toggle the checkbox, so that if the user changes their mind that the end date is different from the start date. Some sort of toggle? How would that work with the above code snippet? Thnx.
Jesse
+2  A: 

Simple hack to solve your problem.

<html>
<head>
    <script src="js/jquery.js" ></script>
</head>
<body>
    <form>
        <input type="text" name="startdate" id="startdate" value=""/>
        <input type="text" name="enddate" id="enddate" value=""/>
        <input type="checkbox" name="checker" id="checker" />
    </form>
    <script>
    $(document).ready(function(){
            $("input#checker").bind("click",function(o){
                if($("input#checker:checked").length){
                    $("#enddate").val($("#startdate").val());
                }else{
                    $("#enddate").val("");
                }
            });
        }
    );
    </script>
</body>
</html>
Tom Schaefer
A: 

Hi Ill try this code to copy my test box but doesn't work. Bellow is my code.. Can anyone help me please.

function copyInfo() {

    var address1=$("#txtAddress").val();
    var address2=$("#txtAddress2").val();
    var postcode=$("#txtPostcode").val();
    var city=$("#txtCity").val();
    var state=$("#ddlCompState").val();
    var country=$("#ddlCompCountry").val();

    if (this.checked == true)
        $("#txtCompAddrs1").val(address1);
        $("#txtCompAddrs2").val(address2);
        $("#txtCompPostcode").val(postcode);
        $("#txtCompCity").val(city);
        $("#ddlCompState").val(state);
        $("#ddlCompCountry").val(country);    
}

Thanks

Diana