0

I have a dropdown which has a value of "Yes" or "No." I have a field that is required if the user selects "No" but even if I choose "Yes" the field still wants a required input. I am using jquery to achieve this but it is still not working. My code is below.

<script type="text/javascript">
  $('ddlBlueStake').change(function () {
    if (this.value == 'No') {
      $('txtBlueStakeComm').prop('required', true);
    } else {
      $('txtBlueStakeComm').prop('required', false);
    }
  });
</script>

On my field I am calling required="true"

<asp:TextBox ID="txtBlueStakeComm" runat="server" TextMode="MultiLine" required="true" CssClass="required"></asp:TextBox>

Any help would be appreciated.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 2
    Hi , this is `txtBlueStakeComm` id ? you missed `#` in your selector .Also what is `ddlBlueStake` id or class ? – Swati Nov 13 '20 at 16:27
  • Both of these are id. I did try removing the attribute but still have same error. I have done something similar with checkbox and it works fine but it will not work with the dropdown. – Philip Herman Nov 13 '20 at 16:43
  • @RyanWilson you can't remove properties from an element. Setting `required` to `false` is correct. – Rory McCrossan Nov 13 '20 at 16:52
  • @PhilipHerman the `ID="txtBlueStakeComm"` is the *server side* id value. When the control is rendered to the client side ASP.Net Webforms will change that value entirely. There are several methods to work around this truly horrendous behaviour. See the duplicate I marked. Also ensure you're running your jQuery code after the DOM has loaded. Given the lack of a document.ready handler in the JS example you need to make sure that ` – Rory McCrossan Nov 13 '20 at 16:53
  • I changed my code to use the client side id like $("[id$=txtBlueStakeComm]").prop('required', false); and moved the code to just before the

    tag but now the form will not submit. No error now but it still will not submit correctly.

    – Philip Herman Nov 13 '20 at 17:16

0 Answers0