Learning
Learning

Reputation: 20001

Scroll to part of page where first validation failed message is showing

I have a page which i validate and i show relevant validation message next to the field.

I want page to scroll to part of the page where first failed validation message is shown when user clicks the save button

Topically i want to scroll to an element with class row-validate which has visibility:visible.

<div class="row form-inputs">
    <div class="col-12">
        <label>Full Name *</label>
        <input name="txtFirstName" type="text" id="txtFirstName" class="form-control">
        <span id="rfFN" class="row-validate" style="visibility:hidden;">Mandatory field</span>
    </div>
</div>

<div class="row form-inputs">
    <div class="col-12">
        <label>Email *</label>
        <input name="txtEmail" type="text" id="txtEmail" class="form-control">
        <span id="rfvEmail" class="row-validate" style="visibility: visible;">Mandatory field</span>
        <span id="revEmail" class="row-validate" style="visibility:hidden;">Invalid email address</span>
    </div>
</div>

<div class="row form-inputs">
    <div class="col-12">
        <label>Mobile *</label>
        <input name="txtPhone" type="text" value="05" id="txtPhone" class="form-control" placeholder="0501234567">
        <span id="rfvphone" class="row-validate" style="visibility:hidden;">Mandatory field</span>
        <span id="revPhone" class="row-validate" style="visibility: visible;">! Enter correct mobile number </span>
    </div>
</div>

<div class="row form-inputs">
    <div class="col-12">
        <label>Company </label>
        <input name="txtCompany" type="text" id="txtCompany" class="form-control">
    </div>
</div>

jQuery

below code will scroll me to first element which has class row-validate but it wont check if it has visibility:visible visibility property as visible or hidden

$("#button").click(function() {
    $([document.documentElement, document.body]).animate({
        scrollTop: $(".row-validate").offset().top
    }, 2000);
});

Upvotes: 1

Views: 835

Answers (2)

Md. Shohag Mia
Md. Shohag Mia

Reputation: 374

Try this ,

$(".row-validate:visible").offset().top

Edited:

$("#button").click(function() {
let visible = $('.row-validate').filter(function() {
    return !($(this).css('visibility') == 'hidden' || 
         $(this).css('display') == 'none');
   });

   $([document.documentElement, document.body]).animate({
        scrollTop: visible[0].offsetParent.offsetTop 
   }, 2000);
 });

OR just

$("#button").click(function() {
  $([document.documentElement, document.body]).animate({
     scrollTop: $('.row-validate:visible[style="visibility:visible;"]').parent().offset().top
  }, 200);
});

This will scroll to the parent element which is child of the body to show the label, input field and the error message . Not only the error message .

Upvotes: 1

Chaska
Chaska

Reputation: 3205

How about using attribute selector?

This is not a best workaround just for your situation.

$("#button").click(function() {
  $([document.documentElement, document.body]).animate({
    scrollTop: $('.row-validate:visible[style="visibility: visible;"]').offset().top
  }, 200);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row form-inputs">
  <div class="col-12">
    <label>Full Name *</label>
    <input name="txtFirstName" type="text" id="txtFirstName" class="form-control">
    <span id="rfFN" class="row-validate" style="visibility:hidden;">Mandatory field</span>
  </div>
</div>

<div class="row form-inputs">
  <div class="col-12">
    <label>Email *</label>
    <input name="txtEmail" type="text" id="txtEmail" class="form-control">
    <span id="rfvEmail" class="row-validate" style="visibility: visible;">Mandatory field</span>
    <span id="revEmail" class="row-validate" style="visibility:hidden;">Invalid email address</span>
  </div>
</div>

<div class="row form-inputs">
  <div class="col-12">
    <label>Mobile *</label>
    <input name="txtPhone" type="text" value="05" id="txtPhone" class="form-control" placeholder="0501234567">
    <span id="rfvphone" class="row-validate" style="visibility:hidden;">Mandatory field</span>
    <span id="revPhone" class="row-validate" style="visibility: visible;">! Enter correct mobile number </span>
  </div>
</div>

<div class="row form-inputs">
  <div class="col-12">
    <label>Company </label>
    <input name="txtCompany" type="text" id="txtCompany" class="form-control">
  </div>
</div>

<button id="button" style="margin-bottom:5000px;">Button</button>

Upvotes: 0

Related Questions