Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
share|improve this question
    
is the script you used working or not? –  Furqan Jun 17 '12 at 8:22
add comment

2 Answers 2

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();
      }
   });
});
share|improve this answer
    
thanks this was really helpful –  sarah Jun 17 '12 at 14:50
    
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
add comment

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

$('#Checkbox').is(':checked')
$('#Checkbox').attr('checked')
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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