0

Below is the jQuery code I'm trying to use to make a hidden text box visible. However when clicked the text box stays hidden, and none of the alerts will fire.

For this check box I have tried .click, .change, and .mousedown however none has worked.

Here is the jQuery

$('#OverrideRegionInd').click(function () {
    alert("here");
    alert($(this).is(':checked'));
    if ($(this).is(':checked')) {
        $('#territoryName').html("");
        $('#Region').val("");
        $('#Region').show();
    } else {

    }
});

And here is the code from the view. This is an ASP MVC 3 site

    <div class="M-editor-label">
        Override Territory Manually?
    </div>
    <div class="M-editor-field" style="padding-right:190px;padding-top:5px;">
        @Html.CheckBoxFor(Model => Model.OverrideRegionInd)
    </div>

EDIT

Here is the entire <div> after it is rendered

    <div class="M-editor-field" style="padding-right:190px;padding-top:5px;">
        <input data-val="true" data-val-required="The Override Territory? field is required." id="OverrideRegionInd" name="OverrideRegionInd" type="checkbox" value="true" /><input name="OverrideRegionInd" type="hidden" value="false" />
    </div>
3
  • inspect the html. does the checkbox have an ID Commented Apr 30, 2013 at 21:41
  • Show me the HTML that rendered Commented Apr 30, 2013 at 21:42
  • 1
    Make sure your JQuery is loaded after the HTML is rendered. Wrap it in a $(document).ready(function () { //your javascript code }); Commented Apr 30, 2013 at 21:51

1 Answer 1

1

The code you posted (and the resulting HTML) works exactly as is (see Fiddle HERE)

The only difference, is JSFiddle wraps that jQuery into a document.ready call, try that:

$(function () {
    $('#OverrideRegionInd').click(function () {
       //click function code
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Putting this code inside the document.ready made it work. Any idea why it won't work outside of it? (I just started learning jQuery on the job about a week ago)
@NealR Because there's no guarantee that the DOM has fully loaded when this code executes (in your case it hadn't), so you should always use document.ready (similar to window.onload in pure JavaScript).

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.