1

I am currently creating a MVC project that takes in data from a form/view generated from the model.

I want to be able to check user input in the form. If that input is less than 1, it would trigger another panel of information to open. The code below isn't quite functioning correctly. How exactly do I make this happen?

In my View:

      <div class="form-group">
           @Html.LabelFor(model => model.Buy2Yearsataddress, htmlAttributes: new { @class = "control-label col-md-2" })
           <div class="col-md-10" id="coBuyerYearsAtAddress">
                   @Html.EditorFor(model => model.Buy2Yearsataddress, new { htmlAttributes = new { @class = "form-control" } })
                   @Html.ValidationMessageFor(model => model.Buy2Yearsataddress, "", new { @class = "text-danger" })
            </div>
      </div>

And here's what's in my script file:

    $("#coBuyerYearsAtAddress").focusout(function () {
         if ($("#coBuyerYearsAtAddress").val() <= 1) {
             $('#coBuyerPreviousResidence').show();
         }
         else {
             $('#coBuyerPreviousResidence').hide();
         }
});
1
  • 1
    Define "isn't quote functioning correctly". What is the code doing? What do you expect it to do? Why? When you step through the code in a debugger, where does it deviate from what you expect? Commented Aug 3, 2017 at 16:57

2 Answers 2

1

Try the following

$('.form-control').change(function(){
    if ($(this).val() <= 1) {
        $('#coBuyerPreviousResidence').show();
    }
    else {
        $('#coBuyerPreviousResidence').hide();
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Second one almost worked. When input is entered, the panel pops up. However, it doesn't check the logic. In other words, I can enter a 0 or 300 and the panel pops up either way. Thoughts? Also, not quite sure exactly what you need as far as what the html is generated. I'd gladly give that info with clarification!
@cryan check now
0

Looks like you are checking the value of coBuyerYearsAtAddress but you should be checking the value of "Buy2Yearsataddress" instead.

1 Comment

This fixed my issue completely. I knew it would be something super simple. Thank you so much! I appreciate it!

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.