0

I am trying to make an asp textbox/checkbox combo that will hide the textbox if the checkbox is checked but I cant get the jQuery/Javascript to work. I have tried many different solutions and havent gotten it to work so any help would be appreciated

ASP:

<div runat="server" id="editStartYear" class="productInfo">
    Edit Start Year:
    <asp:CheckBox ID="startDateCheckBox" runat="server" ClientIDMode="Static" Text="Unknown Start Date"/>
    <asp:TextBox runat="server" ID="startBox" ClientIDMode="Static" Width="150" placeholder="Start Year"></asp:TextBox>
</div>

jQuery:

$(document).ready(function () {

$('#searchIcon').hover(function () {
    $('#searchIcon').attr("src", "includes/images/searchIconHover.png");
}, function () {
    $('#searchIcon').attr("src", "includes/images/searchIcon.png");
});
$('#searchBox').focus(function () {
    $('#searchBox').attr("value", "");
});

$("#startDateCheckBox").change(function () {
        if (this.checked) {
            $("#startBox").hide();
        } else {
            $("#startBox").show();
        }

});

});
1
  • This question could be better by adding the actual HTML output. Commented Jun 8, 2016 at 3:16

1 Answer 1

2

You can add ClientIDMode="Static" in the asp.net markup for the check box and the text box. Then the client side javascript can be used to address the check box. Right now you have it as a server side control.

$("#startDateCheckBox").change(function() {
    if(this.checked) {
        $("#startBox").hide();
    } else {
        $("#startBox").show();
    }

});
Sign up to request clarification or add additional context in comments.

6 Comments

how should I call the function? Should I just put it in the document.ready and get rid of the onclick? Right now with the onclick it is saying ReferenceError: hideStartDiv is not defined
Try having only this code in the document.ready. Comment everything else out. If it does not work, post your entire JavaScript
commenting it didn't solve the problem. I edited the question to have all the javascript including your suggested edits
Remove onclick="hideStartDiv()" from your asp.net markup
Put alert messages or console.log statements and see where the control reaches
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.