1

i need to write a javascript code that make the checkbox if checked view a textbox and if not checked the text box is invisable automatically without having a button to click on it to do that

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <script type="text/javascript">

        $('#Checkbox').click(function(){
            var checked_status = this.checked;
            if(checked_status == true) {
                $('#txt').show();
            }
            else { $('#txt').hide();
            }
        }); </script>
</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <input type="checkbox" id="checkbox"/>
    <input type="text" id="txt" style="display:none"/>
</asp:Content>
  • is the script you used working or not? – Furqan Hameedi Jun 17 '12 at 8:22
6

First: id in html elements and javascript are case sensitive:

#Checkbox ≠ #checkbox 

Second, change event is better than click for a checkbox.

$("#checkbox").change(function() {
   var checked = this.is(":checked");
   if (checked) {
      $("#txt").show();
   }
});

Third, you should wait until your dom is loaded, by declaring your script in $(document).ready:

$(document).ready(function() {
   $("#checkbox").change(function() {
      var checked = $(this).is(":checked");
      if (checked) {
         $("#txt").show();
      }
   });
});
  • what if i wanted to make the same thing in but in – sarah Jun 17 '12 at 16:37
  • if the value in the select should decide if #txt is visible or not, you can use change event, instead of checked you can use val() of the select. – Anatoly Mironov Jun 17 '12 at 20:39
  • yes that is what i did but it didnt work <script> $(document).ready(function () { $("#selection").change(function () { var mySelect = document.myform.myselectbox; </script> <form action="#" id="myform"> Selection: <select id='selection' > <option >sarah</option> <option >mai</option> <option >alaa</option> <option >salma</option> <option id="Other" >laila</option></select> <input type="text" id="txta" style="display:none" /> var selIndex = mySelect.selectedIndex; if ($(this).val() == 'Other') { $('#txta').show(); } else { $('#txta').hide(); } }); }); – sarah Jun 18 '12 at 7:05
  • is there any unchecked keyword (":unchecked") exists like (":checked"). in my case unchecked event doesn't fire. ? anyone pls help, how to trigger a unchecked event, when a checkbox is checked first and then unchecked again. – SaMolPP Oct 27 '15 at 5:40
1

Try this simpler to validate whether the checkbox is checked or not:

$('#Checkbox').is(':checked')
$('#Checkbox').attr('checked')
  • How to capture the unchecked event ? in my case, if i uncheck the already checked checkbox, I want to make a tetxbox's value empty.Is this possible? Is there anything like this exist: var unchecked = $(this).is(":unchecked"); – SaMolPP Oct 27 '15 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.