views:

62

answers:

4
+1  Q: 

jquery question

I am using the Jquery library to copy the text enter in one textfield to another textfield using a check box when clicked.. which is as follows

<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>

now here i want the check box to be selected by default, so that the data entered in start date get copied automatically as check box is checked by default... so what event should be called here in jquery script?

please help me in resolving this issue... Solutions given below is not working for me...

A: 

what about:

$("#startdate").change(function() {
  // code here
});
RC
isnt it true that this event will b functional only when the state of checkbox changes?, I mean I need the event for checkbox selected as default when page is refreshed.. just confirming as per my understanding.. may be I am Wrong
OM The Eternity
No, this event will fire on every "change". For the checkbox to be checked by default use the HTML attribute `checked=checked`
RC
+2  A: 

You would want the value to be copied whenever there is a change, so you can use the keypress event:

function copyDate() {
  if($("#checker").is(":checked")){
    $("#enddate").val($("#startdate").val());
  }
}

$(function(){
  $("#startdate").keypress(copyDate).click(copyDate);
});
Guffa
@Guffa what if i uncheck the checkbox and and change the startdate and then again recheck the checkbox? I did it, but it didn't replicated to enddate, Pls help
OM The Eternity
@OM: Right, you would need the event on `click` also to handle that. I changed the code above.
Guffa
A: 

Your question isn't very clear. It seems to me that you want to have the checkbox checked when the page loads, and that the start date is copied to the end date also:

$(document).ready(function() {
  $("#checker").attr("checked","checked");
  $("#enddate").val($("#startdate").val());
});

EDIT:

combined with your event handler:

$(document).ready(function() {
   function checker() {
       if ($("#checker").is(":checked"))
          $("#enddate").val($("#startdate").val());
       else
          $("#enddate").val("");
   }

   $("#checker")
     .click(checker)
     .attr("checked","checked");

   checker();
});

This looks complicated, but there's a problem with jQuery: when setting the "checked" attribute, firing the event handler right away using .click() will not work because the .is(":checked") function will not return the changed value (I don't know why, but that's how it is).

Philippe Leybaert
+1 for ur correct understanding :) but i need the solution along with functionalty I am using, i mean both ur solution and my click event has to be functional...
OM The Eternity
Its Not working
OM The Eternity
@Philippe ur solutionas are not working, pls help
OM The Eternity
None of the solutions found to be working
OM The Eternity
I've made changes. Tested it and it seems to be working. See http://jsbin.com/itaga3/5
Philippe Leybaert
A: 

First of all you have two options for checkbox to be checked by default... 1) from the html where the checkbox was defined using

checked='checked'

2) from jquery using

$(document).ready(function() { $("#checker").attr("checked","checked"); )};

Now coming to the main point... which is already mentioned above by 'Guffa'

$("#startdate").keypress(function(){
if($("#checker").is(":checked")){
  $("#enddate").val($("#startdate").val());
}

});

booota