How to ignore disable question in HTML form so I will able to move in next section using "Next" button in HTML/JS application?

Viewed 240

I have created a form in which we are asking users a few questions. There are a few sections and in each section there are 5 to 10 questions. Users can disable the question using the NA button.

But they need to answer the remaining question. I am able to create everything but having issues with the Next button. I am not able to move to the next section because of disabled question. It is asking to answer disable question also.

Below is the code. Also, I want to remove any given answer in case the user clicks the NA button.

$(document).ready(function() {
  let ctr = 1;
  $('.answers').each(function(index) {
    let i = index + 1
    let html = ` <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="1">
    <label class="form-check-label" for="gridRadios${ctr}"> Never</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="2">
    <label class="form-check-label" for="gridRadios${ctr}">Rarely</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="3">
    <label class="form-check-label" for="gridRadios${ctr}">Occasionally</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="4">
    <label class="form-check-label" for="gridRadios${ctr}">Often</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="5">
    <label class="form-check-label" for="gridRadios${ctr}">Always</label>
    </div>

    <div class="form-group">
    <input type="button" name="q${i}Remark" value="Remark" onclick="onButtonClick(${i})" />
    <input class="hide" type="text" id="textInput${i}" value="" oninput="updateTextBox()" />
    <p>Remaining Characters: <span id="chars-left">100</span></p>
    </div>
    <div class="form-group">
    <button name="disable${i}" id="na${i}" class='btn-na'>N/A</button>
    </div>
    `
    $(this).html(html);
    ctr++;
  })




  $(".btn-na").click(function() {
    let n = $('.answers.disabled').length
    if (n >= 3) {
      alert('You can only disable 3');
      return
    }
    $(this).closest('.answers').find("input").attr('disabled', true);
    $(this).closest('.answers').addClass('disabled')
  })
});

$('.btnNextS1').click(function() {
  if ($('div.row1:not(:has(:radio:checked))').length) {
    $('div.row1:not(:has(:radio:checked))').parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please Answer this question</div>");
  } else {
    // e.stopPropagation();
    $('ul.nav-tabs li.nav-item     a.active').closest('li').next('li').find('a').trigger('click');
  }
});
<div class="section-1-questions">
  <div style="background-color:greenyellow;"> <b>Section 1:</b> </div><br>
  <fieldset class="form-group">
    <div class="row1">
      <div class="question1">
        <legend id="q1" class="col-form-label col-sm-8 pt-0"><b>1) Question 1</b></legend>
        <div class="col-sm-10 answers">
        </div>
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q2" class="col-form-label col-sm-8 pt-3"><b>2) Question 2</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q3" class="col-form-label col-sm-8 pt-3"><b>3) Question 3</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q4" class="col-form-label col-sm-8 pt-3"><b>4) Question 4</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q5" class="col-form-label col-sm-8 pt-3"><b>5) QUESTION 5</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <div class="form-group">
    <label for="remarks"><b>Remarks / Observations </b></label>
    <input type="name" class="form-control" name="Remarks1" id="remarks1" aria-describedby="nameHelp" placeholder="Please enter your Remarks / Observations">
    <small id="nameHelp" class="form-text text-muted">Please enter your Remarks / Observations about these questions.</small>
  </div>
</div>
<br>
<a class="btn btn-primary btnPrevious">Previous</a>
<a class="btn btn-primary btnNextS1">Next</a>
</div> 

2 Answers

Just a simple change of selectors does the job.

:not(:has(input[disabled]))

Change your current invalid question selector from:

$('div.row1:not(:has(:radio:checked))')

to:

$('div.row1:not(:has(:radio:checked)):not(:has(input[disabled]))')

This selector makes use of the CSS attribute selector to check whether each question has an input with the disabled attribute (:has(input[disabled])).

$(document).ready(function() {
  let ctr = 1;
  $('.answers').each(function(index) {
    let i = index + 1
    let html = ` <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="1">
    <label class="form-check-label" for="gridRadios${ctr}"> Never</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="2">
    <label class="form-check-label" for="gridRadios${ctr}">Rarely</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="3">
    <label class="form-check-label" for="gridRadios${ctr}">Occasionally</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="4">
    <label class="form-check-label" for="gridRadios${ctr}">Often</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="5">
    <label class="form-check-label" for="gridRadios${ctr}">Always</label>
    </div>

    <div class="form-group">
    <input type="button" name="q${i}Remark" value="Remark" onclick="onButtonClick(${i})" />
    <input class="hide" type="text" id="textInput${i}" value="" oninput="updateTextBox()" />
    <p>Remaining Characters: <span id="chars-left">100</span></p>
    </div>
    <div class="form-group">
    <button name="disable${i}" id="na${i}" class='btn-na'>N/A</button>
    </div>
    `
    $(this).html(html);
    ctr++;
  })




  $(".btn-na").click(function() {
    let n = $('.answers.disabled').length
    if (n >= 3) {
      alert('You can only disable 3');
      return
    }
    $(this).closest('.answers').find("input").attr('disabled', true);
    $(this).closest('.answers').addClass('disabled')
  })
});

$('.btnNextS1').click(function() {
  if ($('div.row1:not(:has(:radio:checked)):not(:has(input[disabled]))').length) {
    $('div.row1:not(:has(:radio:checked)):not(:has(input[disabled]))').parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please Answer this question</div>");
  } else {
    // e.stopPropagation();
    console.log('valid');
    $('ul.nav-tabs li.nav-item     a.active').closest('li').next('li').find('a').trigger('click');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section-1-questions">
  <div style="background-color:greenyellow;"> <b>Section 1:</b> </div><br>
  <fieldset class="form-group">
    <div class="row1">
      <div class="question1">
        <legend id="q1" class="col-form-label col-sm-8 pt-0"><b>1) Question 1</b></legend>
        <div class="col-sm-10 answers">
        </div>
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q2" class="col-form-label col-sm-8 pt-3"><b>2) Question 2</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q3" class="col-form-label col-sm-8 pt-3"><b>3) Question 3</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q4" class="col-form-label col-sm-8 pt-3"><b>4) Question 4</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q5" class="col-form-label col-sm-8 pt-3"><b>5) QUESTION 5</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <div class="form-group">
    <label for="remarks"><b>Remarks / Observations </b></label>
    <input type="name" class="form-control" name="Remarks1" id="remarks1" aria-describedby="nameHelp" placeholder="Please enter your Remarks / Observations">
    <small id="nameHelp" class="form-text text-muted">Please enter your Remarks / Observations about these questions.</small>
  </div>
</div>
<br>
<a class="btn btn-primary btnPrevious">Previous</a>
<a class="btn btn-primary btnNextS1">Next</a>
</div>

This was a bit messy so i rewrote logic for you:

$('.btnNextS1').click(function() {
//on click
  $(".validation").remove()
  // reset all validation added previously

  document.querySelectorAll(".answers").forEach(a => {
    // for each answers
    if (!a.classList.contains("disabled") && $(a).find(":radio:checked").length == 0) {
      // for each if it does not have disabled class and has no radios checked
      $(a).parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please Answer this question</div>");
      // add validation massage to that particular anwser
    }
  })

  if ($(".validation").length === 0) {
    // if no validation errors are present continue
    $('ul.nav-tabs li.nav-item a.active').closest('li').next('li').find('a').trigger('click');
    console.log("triger a")
  }


});

This adds warning message only if no radios are checked and not an N/A, if all is good it removes warning messages.

It also checks if any warning messages are present, if there are no such it triggers next click.

$(document).ready(function() {
  let ctr = 1;
  $('.answers').each(function(index) {
    let i = index + 1
    let html = ` <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="1">
    <label class="form-check-label" for="gridRadios${ctr}"> Never</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="2">
    <label class="form-check-label" for="gridRadios${ctr}">Rarely</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="3">
    <label class="form-check-label" for="gridRadios${ctr}">Occasionally</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="4">
    <label class="form-check-label" for="gridRadios${ctr}">Often</label>
    </div>
    <div class="form-check-inline section-1">
    <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="5">
    <label class="form-check-label" for="gridRadios${ctr}">Always</label>
    </div>

    <div class="form-group">
    <input type="button" name="q${i}Remark" value="Remark" onclick="onButtonClick(${i})" />
    <input class="hide" type="text" id="textInput${i}" value="" oninput="updateTextBox()" />
    <p>Remaining Characters: <span id="chars-left">100</span></p>
    </div>
    <div class="form-group">
    <button name="disable${i}" id="na${i}" class='btn-na'>N/A</button>
    </div>
    `
    $(this).html(html);
    ctr++;
  })


  $(".btn-na").click(function() {
    let n = $('.answers.disabled').length
    if (n >= 3) {
      alert('You can only disable 3');
      return
    }
    $(this).closest('.answers').find("input").attr('disabled', true);
    $(this).closest('.answers').addClass('disabled')
  })

});






$('.btnNextS1').click(function() {

  $(".validation").remove()
  // reset all validation added

  document.querySelectorAll(".answers").forEach(a => {
    // for each answer
    if (!a.classList.contains("disabled") && $(a).find(":radio:checked").length == 0) {
      // if it does not have disabled class and has no radios checked
      $(a).parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please Answer this question</div>");
      // add validation massage
    }
  })

  if ($(".validation").length === 0) {
    // if no validation errros are present continew
    $('ul.nav-tabs li.nav-item a.active').closest('li').next('li').find('a').trigger('click');
    console.log("triger a")
  }


});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section-1-questions">
  <div style="background-color:greenyellow;"> <b>Section 1:</b> </div><br>
  <fieldset class="form-group">
    <div class="row1">
      <div class="question1">
        <legend id="q1" class="col-form-label col-sm-8 pt-0"><b>1) Question 1</b></legend>
        <div class="col-sm-10 answers">
        </div>
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q2" class="col-form-label col-sm-8 pt-3"><b>2) Question 2</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q3" class="col-form-label col-sm-8 pt-3"><b>3) Question 3</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q4" class="col-form-label col-sm-8 pt-3"><b>4) Question 4</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q5" class="col-form-label col-sm-8 pt-3"><b>5) QUESTION 5</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <div class="form-group">
    <label for="remarks"><b>Remarks / Observations </b></label>
    <input type="name" class="form-control" name="Remarks1" id="remarks1" aria-describedby="nameHelp" placeholder="Please enter your Remarks / Observations">
    <small id="nameHelp" class="form-text text-muted">Please enter your Remarks / Observations about these questions.</small>
  </div>
</div>
<br>
<a class="btn btn-primary btnPrevious">Previous</a>
<a class="btn btn-primary btnNextS1">Next</a>
</div>

Related