You are viewing limited content. For full access, please sign in.

Question

Question

Removing required error

asked on April 4, 2018 Show version history

Good morning,

I have a small piece of Javascript that checks a checkbox automatically based on the value of a drop-down menu. For simplicity purposes let's say that the checkbox gets checked when the value of a drop-down menu is "something". The checkbox is required.

The issue that I'm running into is that when you allow the "required" error to show on the checkbox on purpose, say by trying to submit the form without checking the box, the error for "Value is required" as expected. Then when I switch the drop-down to the value "something", the checkbox is checked, but the error persists. 

The form will submit without issues, but I would like for the required error to be removed, to avoid confusion.

I tried to trigger the change, but that didn't work. Any ideas? Below is my simple code and screenshot.

UPDATE: I'm trying trigger the click as well, not sure if that's the right approach.

$( document ).ready(function() {

    $(document).on('change blur', '.dropdown select', chkBoxRA);
  
  function chkBoxRA () {
    
    var dropdown = $('.dropdown select').val();
    
    if (dropdown == 'something') {
      
      $('#Field1-0').attr("checked", true);
      $('#Field1-0').prop("checked", true);
     
    }

	$('#Field1-0').trigger("change");
    
  }

});

 

checkboxError.PNG
0 0

Replies

replied on April 4, 2018 Show version history

Ah!  a little sloppy, but this one seems to work for now.

$( document ).ready(function() {

    $(document).on('change blur', '.dropdown select', chkBoxRA);
  
  function chkBoxRA () {
    
    var dropdown = $('.dropdown select').val();
    
    if (dropdown == 'something') {
      
         if (!$("#Field1-0").is(":checked")) {

            $('#Field1-0').trigger("click");

          }
 
    }


  } // end function

});

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.