I earlier asked another question here
However, I later realized that my requirements are a little different. The above referenced solution takes me only to the error div but I need to scroll up slightly above that to the label for the input text field. Below is the html.
<label class="label_required" for="phone_num_key"></label>
<table class="table_Box">
<tbody>
<tr id="someId" class="redAlert">
<Some more mark up here>
</tr>
</tbody>
</table>
How can I scroll up to the label instead of the tr element with class ‘redAlert’
I tried the below but it didn’t work for me.
var errorDiv= $('.redAlert:visible').first();
var errorLabel = errorDiv.parent("table").prev("label");
// also tried this
//var errorLabel = errorDiv.closest("label").prev();
var scrollPosition = (errorLabel.offset().top);
$(window).scrollTop(scrollPosition);
Looks like the .offeset() function cannot be called on Labels ?
Edit – Please note that there are multiple such labels on the form so I cannot basically use the class selector
offset()does work. problem isparent().use
errorDiv.parents('table').prev('label');FYI,